[R] Cross-validating two matrices

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Jul 27 21:35:26 CEST 2009


Hi,

On Jul 27, 2009, at 3:29 PM, Brian McCarthy wrote:

> Hello,
>
> I am trying to help a colleague with an R problem (see below) to  
> whit I can only generate a very inelegant solution. Any advice would  
> be welcome.
>
> Thanks,
> Brian
>
> If you have two matrices, say a species by trait matrix (Matrix 1  
> below) and a plot by species matrix (Matrix 2 below), is there a  
> straightforward way to prune one matrix so that the species list  
> matches those in a second matrix? Ideally, I need to prune both  
> matrices so that they include only the species found in both. In  
> this case, the two matrices would therefore include only sp1,sp4,sp5.
>
> Thank you so much for any suggestions!
>
> E.g.:
> Matrix 1:
> 	Trait1
> sp1 1.0
> sp2 1.2
> sp4 3.1
> sp5 4.0
> sp7 4.5
>
> Matrix 2
> 		sp1	sp3 sp4 sp5 sp6
> plot1	1	2	5	1	0
> plot2	3	0	1	2	5	
> plot3	1	1	2	3	1
> plot4	0	1	2	1	0

keep <- intersect(rownames(matrix1), colnames(matrix2))
m1 <- matrix1[keep,]
m2 <- matrix2[,keep]

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list