[R] Need help on matrix calculation

Christofer Bogaso bogaso.christofer at gmail.com
Mon Apr 29 15:38:59 CEST 2013


Hello again,

Let say I have 1 matrix:

Mat <- matrix(1:12, 4, 3)
rownames(Mat) <- letters[1:4]

Now I want to subscript of Mat in following way:

Subscript_Vec <- c("a", "e", "b", "c")

However when I want to use this vector, I am geting following error:

Mat[Subscript_Vec, ]
Error: subscript out of bounds

Basically I want to get my final matrix in following way:

  V1 V2 V3
a  1  5  9
e NA NA NA
b  2  6 10
c  3  7 11

i.e. if some of the element(s) in 'Subscript_Vec' is not in 'Mat' then
that row would be filled by NA, WITHOUT altering the sequence of
'Subscript_Vec'

Is there any direct way to achieve that?

Thanks and regards,



More information about the R-help mailing list