Search code examples
rmatrixvectorization

Extracting values from a matrix using a vector of rows and a vector of columns


I am trying to do vectorization and avoid for loops. I have two vectors and a matrix. Further I have a results data.frame in which I am saving for Each ID the corresponding value of the vectors. Further I would like to get for each ID the corresponding value of the matrix which is matrix[vector1[ID],vector2[ID]]. However I would like to do this in a vectorized form.

My Data:

vector1 <- sample(1:5, 10, 1)
vector2 <- sample(1:3,10,1)
df <- data.frame(v1=sample(1:10,5,1),
                 v2=sample(1:10,5,1),
                 v3=sample(1:10,5,1))

results <- data.frame(ID=1:10,
                      vector1,
                      vector2
                      )

for each ID, I want df[vector1[1],vector2[1]], but in a vectorized form such that I can save it in results directly.

I tried the following: results$new=df[vector1,vector2] but I did not get the result I wanted. I want something like this:

ID  vector1   vector2     new
 1        2         3       4 

where the column new is calculated by df[vector1[1],vector2[1]]

ID  vector1   vector2     new
 1        2         3       4 
 2        3         6       2
 3        1         1       1      

Is there a way to do this?


Solution

  • It is a lesser-known feature but R allows you to index matrices using a special syntax - indexing object can be a matrix of 2 columns, where the first column specifies selected rows and the second column specifies columns. In your case:

    results$new <- df[cbind(results$vector1, results$vector2)]
    

    You can read more about it in help("["), the relevant section is here:

    When indexing arrays by ‘[’ a single argument ‘i’ can be a matrix with as many columns as there are dimensions of ‘x’; the result is then a vector with elements corresponding to the sets of indices in each row of ‘i’.