[R] Need help on matrix calculation

arun smartpink111 at yahoo.com
Mon Apr 29 16:35:59 CEST 2013


r
#  [,1] [,2] [,3]
#a    1    5    9
#e    3    7   11
#b    4    8   12
#c   NA   NA   NA
I guess you meant: 
 r1<- Mat[match(Subscript_Vec,rownames(Mat)),]
rownames(r1)<- Subscript_Vec
 r1
#  [,1] [,2] [,3]
#a    1    5    9
#e   NA   NA   NA
#b    2    6   10
#c    3    7   11
A.K.



----- Original Message -----
From: Jorge I Velez <jorgeivanvelez at gmail.com>
To: Christofer Bogaso <bogaso.christofer at gmail.com>
Cc: r-help <r-help at r-project.org>
Sent: Monday, April 29, 2013 9:45 AM
Subject: Re: [R] Need help on matrix calculation

Christofer,

The following should get you started:

r <- Mat[match(rownames(Mat), Subscript_Vec),]
rownames(r) <- Subscript_Vec
r

HTH,
Jorge.-



On Mon, Apr 29, 2013 at 11:38 PM, Christofer Bogaso <
bogaso.christofer at gmail.com> wrote:

> 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,
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list