[R] How to extract from a matrix based on indices in a vector?

Logickle logickledoug at yahoo.com
Wed Jun 10 19:36:31 CEST 2009


Sorry, there may be some lingo for describing the extraction problem I have,
but I don't know it. 

I have a matrix of 2 rows and N columns, and a vector of N 1s and 2s. 

Matrix M: 

      1     2     3      4   ...  N 
1    A     C     D      G  ... 
2    B     D     F      H   ... 

Vector v: 

1  2  2  1  2  1  1 ... N 

I'd like to apply v to M to get 

Vector w: 

A  D  F  G ... 

I.e. each element of v is interpreted as a row-index used to extract that
row's value from the corresponding column in M into the corresponding column
of w.   

Also eventually nrow(M) > 2, in which case the value of the elements of v
would range over 1:nrow(M). 

Seems it should be simple, but maybe not? 

Thanks! 

Doug

PS : I originally posted this on "R devel", then realized my mistake. "Move
thread" didn't work when I tried it, though.
-- 
View this message in context: http://www.nabble.com/How-to-extract-from-a-matrix-based-on-indices-in-a-vector--tp23967316p23967316.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list