[R] extract columns of a matrix/data frame
Marc Schwartz
marc_schwartz at comcast.net
Tue Jul 31 20:15:24 CEST 2007
On Tue, 2007-07-31 at 10:35 -0700, yuvika wrote:
> Hello all,
>
> I have a matrix whose column names look like
>
> a1 a2 b1 b2 b3 c1 c2
> 1 2 3 7 1 3 2
> 4 6 7 8 1 4 3
>
> Now, I can have any number of a's. not just two as shown above and
> same goes for b's and c's. I need to extract all the a's columns and
> put them in another matrix, extract all b's columns and put them in
> some matrix and same goes for "c". How can I identify such pattern and
> get subsets of this matrix depending on columns names?
>
> I will appreciate a quick reply.
> Thanks a lot.
If 'MAT' is your matrix:
> MAT
a1 a2 b1 b2 b3 c1 c2
[1,] 1 2 3 7 1 3 2
[2,] 4 6 7 8 1 4 3
You can use:
> sapply(letters[1:3], function(x) MAT[, grep(x, colnames(MAT))])
$a
a1 a2
[1,] 1 2
[2,] 4 6
$b
b1 b2 b3
[1,] 3 7 1
[2,] 7 8 1
$c
c1 c2
[1,] 3 2
[2,] 4 3
which returns a list containing the three matrices as a consequence of
subsetting 'MAT" based upon the colnames.
This uses sapply() to loop over letters[1:3], which is:
> letters[1:3]
[1] "a" "b" "c"
and then uses grep() to get the indices of the colnames matching the
individual letters, passed as 'x' in each iteration of the sapply()
loop. For example:
> grep("a", colnames(MAT))
[1] 1 2
You can then manipulate each sub-matrix in the list as you require.
See ?sapply, ?grep and ?letters
HTH,
Marc Schwartz
More information about the R-help
mailing list