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. --------------------------------- [[alternative HTML version deleted]]
You can use the grep function to obtain the column indices matching your specified criteria. For example, > r1<-c(1,2,3,7,1,3,2) > r2<-c(4,5,7,8,1,4,3) > test<-matrix(c(r1,r2),byrow=TRUE) > colnames(test)<-c("a1","a2","b1","b2","b3","c1","c2") > test a1 a2 b1 b2 b3 c1 c2 [1,] 1 2 3 7 1 3 2 [2,] 4 5 7 8 1 4 3 > grep("a",(colnames(test))) [1] 1 2 test.a<-test[,grep("a",(colnames(test)))] test.a a1 a2 [1,] 1 2 [2,] 4 5 On Jul 31, 2007, at 10:35 AM, 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. > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >
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.mymat <- matrix(runif(60), ncol=6) colnames(mymat) <- c("a1","a2","b1","b2","c1","c2") mymat a1 a2 b1 b2 c1 c2 [1,] 0.73623481 0.25204019 0.332436396 0.36629507 0.39517285 0.62491949 [2,] 0.48867382 0.20933245 0.511805497 0.03142542 0.82168732 0.20550784 [3,] 0.89198874 0.24477456 0.629644977 0.23442137 0.17828551 0.29640615 [4,] 0.99222414 0.49044514 0.571213786 0.91068115 0.09484414 0.78108139 [5,] 0.66615787 0.13183354 0.004350679 0.32443025 0.38742483 0.76044740 [6,] 0.06642704 0.96257552 0.189716240 0.83969989 0.53470898 0.28319039 [7,] 0.31172264 0.20201281 0.577353264 0.62082694 0.31649255 0.40977000 [8,] 0.52890283 0.46576510 0.107363256 0.72534897 0.12038182 0.06295499 [9,] 0.55292555 0.76459699 0.212533012 0.73275529 0.98008863 0.85302931 [10,] 0.84320369 0.09958472 0.158443155 0.92321443 0.50935938 0.08514859 mymat[,grep("^a", colnames(mymat))] a1 a2 [1,] 0.73623481 0.25204019 [2,] 0.48867382 0.20933245 [3,] 0.89198874 0.24477456 [4,] 0.99222414 0.49044514 [5,] 0.66615787 0.13183354 [6,] 0.06642704 0.96257552 [7,] 0.31172264 0.20201281 [8,] 0.52890283 0.46576510 [9,] 0.55292555 0.76459699 [10,] 0.84320369 0.09958472 ?grep> --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Sorry. There was a mistake in my previous code. Please disregard it and use the following: > r1=c(1,2,3,7,1,3,2) > r2=c(4,5,7,8,1,4,3) > test=matrix(c(r1,r2),nrow=2,ncol=7,byrow=TRUE) > colnames(test)<-c("a1","a2","b1","b2","b3","c1","c2") > test [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 2 3 7 1 3 2 [2,] 4 5 7 8 1 4 3 > grep("a",(colnames(test))) [1] 1 2 > test.a<-test[,grep("a",(colnames(test)))] > test.a a1 a2 [1,] 1 2 [2,] 4 5 Kyle H. Ambert Graduate Student, Dept. Behavioral Neuroscience Oregon Health & Science University ambertk@ohsu.edu On Jul 31, 2007, at 10:35 AM, 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. > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
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:> MATa1 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
On Tue, 2007-07-31 at 11:47 -0700, yuvika wrote:> Hello, > > Thanks for the immediate help. However, I have a question for you. > let's say the matrix looks like this > > name a1 a2 b1 b2 c1 c2 > 0 4 2 7 8 1 2 > 0 3 6 9 2 2 9 > 1 2 7 9 2 4 2 > 1 3 2 2 6 7 8 > 2 2 7 8 3 4 2 > 3 4 6 8 9 0 2 > 3 6 8 9 3 6 7 > > Now, what i want to do is still make submatrices but now make 3 > matrices(based on a,b,c just like before) for name=0, 3 matrices for > name=1 and so on.. > how can i do this? > > looking forward for your help. > thanks > yuvikaYuvika, Please be sure to 'reply to all' so that the list thread stays intact and can be of benefit to others in the archive. Otherwise knowledge transfer is lost... In this case, we can split() the initial matrix based upon the 'name' column and then still use the initial solution, with modifications. In effect, we end up with 'nested' loops:> MATname a1 a2 b1 b2 c1 c2 [1,] 0 4 2 7 8 1 2 [2,] 0 3 6 9 2 2 9 [3,] 1 2 7 9 2 4 2 [4,] 1 3 2 2 6 7 8 [5,] 2 2 7 8 3 4 2 [6,] 3 4 6 8 9 0 2 [7,] 3 6 8 9 3 6 7 We first need to coerce the matrix to a data frame to use this approach: DF <- as.data.frame(MAT)> DFname a1 a2 b1 b2 c1 c2 1 0 4 2 7 8 1 2 2 0 3 6 9 2 2 9 3 1 2 7 9 2 4 2 4 1 3 2 2 6 7 8 5 2 2 7 8 3 4 2 6 3 4 6 8 9 0 2 7 3 6 8 9 3 6 7 # split() DF by the 'name' column # strip the 'name' column while we are at it DF.split <- split(DF[, -1], DF$name)> DF.split$`0` a1 a2 b1 b2 c1 c2 1 4 2 7 8 1 2 2 3 6 9 2 2 9 $`1` a1 a2 b1 b2 c1 c2 3 2 7 9 2 4 2 4 3 2 2 6 7 8 $`2` a1 a2 b1 b2 c1 c2 5 2 7 8 3 4 2 $`3` a1 a2 b1 b2 c1 c2 6 4 6 8 9 0 2 7 6 8 9 3 6 7 Now use lapply() to navigate the above list, then use the initial solution with lapply() instead of sapply() on each data frame within the list: RES <- lapply(DF.split, function(x) sapply(letters[1:3], function(i) x[, grep(i, colnames(x))]))> RES$`0` $`0`[[1]] a1 a2 1 4 2 2 3 6 $`0`[[2]] b1 b2 1 7 8 2 9 2 $`0`[[3]] c1 c2 1 1 2 2 2 9 $`1` $`1`[[1]] a1 a2 3 2 7 4 3 2 $`1`[[2]] b1 b2 3 9 2 4 2 6 $`1`[[3]] c1 c2 3 4 2 4 7 8 $`2` $`2`[[1]] a1 a2 5 2 7 $`2`[[2]] b1 b2 5 8 3 $`2`[[3]] c1 c2 5 4 2 $`3` $`3`[[1]] a1 a2 6 4 6 7 6 8 $`3`[[2]] b1 b2 6 8 9 7 9 3 $`3`[[3]] c1 c2 6 0 2 7 6 7 HTH, Marc