pankaj borah
2011-Apr-13 16:13 UTC
[R] Extracting selected rows from a matrix to a submatix
I have a matrix M> dim(M)[1] 30380 561 I have another list L contains , that contains some row names of matrix M str (L) chr [1:21037] Now I want to extract the submatrix subM (21037 , 561) from the matrix M by matching the rownames (M) to the 21037 rownames o f L How do I do that ? Thanks and regards, Pankaj Barah Pankaj Barah Department of Biology Norwegian University of Science & Technology (NTNU) Realfagbygget, N-7491 Trondheim, Norway T [[alternative HTML version deleted]]
Dennis Murphy
2011-Apr-14 12:54 UTC
[R] Extracting selected rows from a matrix to a submatix
Hi:
Here's an example:
m <- as.data.frame(matrix(rnorm(10000), nrow = 100))
dim(m)
[1] 100 100
# Assign rownames to rows of m
rownames(m) <- paste('A', 1:100, sep = '')
# Select a random subset of the rownames
vn <- sample(rownames(m), 50)
# Subset m by selecting the rownames from vn
m2 <- m[rownames(m) %in% vn, ]
# Did we get the right ones?
identical(sort(vn), sort(rownames(m2)))
[1] TRUE
HTH,
Dennis
On Wed, Apr 13, 2011 at 9:13 AM, pankaj borah
<pankajborah2k3@yahoo.co.in>wrote:
> I have a matrix M
>
> > dim(M)
>
> [1] 30380 561
>
> I have another list L contains , that contains some row names of matrix M
> str (L)
> chr [1:21037]
>
> Now I want to extract the submatrix subM (21037 , 561) from the
> matrix M by matching the rownames (M) to the 21037 rownames o f L
>
> How do I do that ?
>
> Thanks and regards,
>
> Pankaj Barah
>
>
>
>
> Pankaj Barah Department of Biology
> Norwegian University of Science & Technology (NTNU)
> Realfagbygget, N-7491 Trondheim, Norway T
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@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]]