[R] selective subsetting of a correlation matrix

Romain Francois romain.francois at dbmail.com
Fri Dec 4 14:24:29 CET 2009


On 12/04/2009 02:18 PM, Lee William wrote:
>
> Dear All,
> I have a correlation matrix say 'M' (4000x4000) for 4000 genes and I want to
> subset it to 'N' (190x190) for 190 genes.
> The list of those 190 genes are in variable 't'. So the idea is to read the
> names of genes from variable 't' and subset the matrix M accordingly.
> Any thoughts are welcome!
>
> Best
> Lee

Hi,

Assuming the correlation matrix has dimnames, you can do :

x[ rownames(x) %in% t, colnames(x) %in% t ]

Here is a reproducible example, please make sure you provide one next time.

x <- expand.grid( x1 = 1:2, x2 = 1:2, x3 = 1:2, x4 = 1:2 )
y <- cor( x )
t <- c("x1", "x2")
y[ rownames(y) %in% t, colnames(y) %in% t ]

Romain


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/Gq7i : ohloh
|- http://tr.im/FtUu : new package : highlight
`- http://tr.im/EAD5 : LondonR slides




More information about the R-help mailing list