Return value from a column specified in the same row

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)) #To pre-allocate memory
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-, .

!

+3
3
t(data[,1:30])[30*(0:399999)+data[,31]]

, , ( 400000 * 31 ), . , .

+2

Singe-index . - :

i <- nrow(data)*(data[,31]-1) + 1:nrow(data)
a <- data[i]

R. . .

## create a random (10 x 5) matrix                                                                                                                           
M <- matrix(rpois(50,50),10,5)
## use the last column to index the maximum value of the first 5                                                                                             
## columns                                                                                                                                                   
MM <- cbind(M,apply(M,1,which.max))
##             column ID          row ID                                                                                                                     
i <- nrow(MM)*(MM[,ncol(MM)]-1) + 1:nrow(MM)
all(MM[i] == apply(M,1,max))

, , , , :

ii <- cbind(1:nrow(MM),MM[,ncol(MM)])
all(MM[ii] == apply(M,1,max))
0

:

M <- matrix(rpois(30*400000,50),400000,30)
MM <- cbind(M,apply(M,1,which.max))
a <- rep(0,nrow(MM))
for (i in 1:(ncol(MM)-1)) {
    a[MM[, ncol(MM)] == i] <- MM[MM[, ncol(MM)] == i, i]
}

i, i. , a.

0
source

Source: https://habr.com/ru/post/1753479/


All Articles