Crosby, Jacy R
2009-May-11 20:53 UTC
[R] Looking for a quick way to combine rows in a matrix
I'm working with genotype data in a frequency table:> a=matrix(1:16, nrow=4) > rownames(a)=c("AA","AT","TA","TT") > a[,1] [,2] [,3] [,4] AA 1 5 9 13 AT 2 6 10 14 TA 3 7 11 15 TT 4 8 12 16 'AT' and 'TA' are essentially the same, and I'd like to combine (add) the rows to reflect this. The final matrix should be: [,1] [,2] [,3] [,4] AA 1 5 9 13 AT 5 13 21 29 TT 4 8 12 16 Is there a fast way to do this? Thanks in advance! Jacy Crosby jacy.r.crosby@uth.tmc.edu [[alternative HTML version deleted]]
Try this:> key <- rownames(a) > key[key == "AT"] <- "TA" > do.call(rbind, by(a, key, colSums))V2 V3 V4 V5 AA 1 5 9 13 TA 5 13 21 29 TT 4 8 12 16 On Mon, May 11, 2009 at 4:53 PM, Crosby, Jacy R <Jacy.R.Crosby@uth.tmc.edu>wrote:> I'm working with genotype data in a frequency table: > > > a=matrix(1:16, nrow=4) > > rownames(a)=c("AA","AT","TA","TT") > > a > [,1] [,2] [,3] [,4] > AA 1 5 9 13 > AT 2 6 10 14 > TA 3 7 11 15 > TT 4 8 12 16 > > 'AT' and 'TA' are essentially the same, and I'd like to combine (add) the > rows to reflect this. The final matrix should be: > > [,1] [,2] [,3] [,4] > AA 1 5 9 13 > AT 5 13 21 29 > TT 4 8 12 16 > > Is there a fast way to do this? > > Thanks in advance! > > Jacy Crosby > jacy.r.crosby@uth.tmc.edu > > > [[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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Johannes Hüsing
2009-May-12 17:27 UTC
[R] Looking for a quick way to combine rows in a matrix
jim holtman schrieb:> Try this: > > >> key <- rownames(a) >> key[key == "AT"] <- "TA" >> do.call(rbind, by(a, key, colSums)) >>something like paste(sort(strsplit(key, split="")[[1]]), "") might be more general.
Jorge Ivan Velez
2009-May-13 01:01 UTC
[R] Looking for a quick way to combine rows in a matrix
Dear Jacy, If AT and TA always one after the other, you might consider the following as an alternative: res <- apply(a, 2, function(x) c(x[1], sum(x[2:3]), x[4] )) rownames(res) <- rownames(a)[-3] res #[,1] [,2] [,3] [,4] #AA 1 5 9 13 #AT 5 13 21 29 #TT 4 8 12 16 HTH, Jorge On Mon, May 11, 2009 at 4:53 PM, Crosby, Jacy R <Jacy.R.Crosby@uth.tmc.edu>wrote:> I'm working with genotype data in a frequency table: > > > a=matrix(1:16, nrow=4) > > rownames(a)=c("AA","AT","TA","TT") > > a > [,1] [,2] [,3] [,4] > AA 1 5 9 13 > AT 2 6 10 14 > TA 3 7 11 15 > TT 4 8 12 16 > > 'AT' and 'TA' are essentially the same, and I'd like to combine (add) the > rows to reflect this. The final matrix should be: > > [,1] [,2] [,3] [,4] > AA 1 5 9 13 > AT 5 13 21 29 > TT 4 8 12 16 > > Is there a fast way to do this? > > Thanks in advance! > > Jacy Crosby > jacy.r.crosby@uth.tmc.edu > > > [[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]]