I am stuck in a simple loop that takes more than an hour to work, and I need help to speed it up.
Basically, I have a matrix with 31 columns and 400,000 rows. The first 30 columns have values, and the 31st column has a column number. I need each row to retrieve a value in the column indicated by the 31st column.
Example row: [26,354,72,5987 .., 461,3] (this means that the value in column 3 is requested after (72))
An overly slow cycle looks like this:
a <- rep(0,nrow(data))
for (i in 1:nrow(data)) {
a[i] <- data[i,data[i,31]]
}
I would think this would work:
a <- data[,data[,31]]
... but this leads to the error "Error: cannot allocate a 2.8 MB vector".
, , , , , , , - R.
, a-, .
!