I have the following issue. I have to compare data in a matrix (datatable or dataframe) using the following function:
Q <- function(j){
# j = (1:dim(x)[1])[1]
Q1 <- c()
for(i in 2:ncol(x)){
# i = 2
item <- x[j,1]
indices <- which(x[,i] == item)
items <- x[1:indices, i]
Q1 <- c(Q1, items)
}
return(Q1)
}
For example with a data like this:
tablero <- data.frame(t1 = c(1,2,3),
t2 = c(3,1,2),
t3 = c(3,2,1))
I get this output:
Q(1) = 3 1 3 2 1
Q(2) = 3 1 2 3 2
Q(3) = 3 3
The thing is that i have a big matrix with of 50.000 rows and 7 columns and that function is too slow and use a lot of memory. Is there a more optimal way in memory use and speed to do that same thing?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…