Re: [R] selecting matrix cels by two factors

From: Berton Gunter <gunter.berton_at_gene.com>
Date: Sat 17 Dec 2005 - 10:29:41 EST


You can subscript any arbitrary array (of any dim) with an appropriately dimensioned matrix of integer indices. So all you have to do is convert the dimnames into indices. One simple way to do this is:

X[cbind(match(c("D","E","F","D","E","F"),rownames(X)),

        match(c("A","C","A","B","B","C"),colnames(X)))]

This gives a vector, which you can dimension however you like.

"The business of the statistician is to catalyze the scientific learning process." - George E. P. Box    

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Dimitri Joe
> Sent: Friday, December 16, 2005 3:02 PM
> To: R-Help
> Subject: [R] selecting matrix cels by two factors
>
> Hi,
>
> I have a (numeric) matrix X of the type
>
> > X <- matrix(c(1:9),ncol=3,nrow=3)
> > colnames(X) <- c("A","B","C")
> > rownames(X) <- c("D","E","F")
>
> Also, a have a data frame Y like
>
> > Y <- as.data.frame(cbind( c("D","E","F","D","E","F"),
> + c("A","C","A","B","B","C") ) )
>
> I want a matrix like
>
> 1 4 X["D","A"] X["D","B"]
> 8 5 or, equivalentely, X["E","C"] X["E","B"]
> 3 9 X["F","A"] X["F","C"]
>
>
> Any suggestions?
> Thanks in advanced,
>
> Dimitri
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Dec 17 10:43:10 2005

This archive was generated by hypermail 2.1.8 : Sat 17 Dec 2005 - 14:34:09 EST