Dear Sirs/Madam, I am a beginner to R, and I am currently working on a data matrix which looks like this:> head(oligo)ko:K00001 ko:K00003 ko:K00005 ko:K00008 ko:K00009 ko:K00010 ko:K00012 AAA 370 631 365 67 164 455 491 KAA 603 1208 170 157 68 495 922 NAA 60 110 10 7 44 51 94 DAA 183 802 41 62 26 166 263 CAA 58 12 58 1 1 23 8 TAA 451 468 201 90 131 320 518 For certain specific columns, I need to merge the columns in such a way that the elements get added together. For example, in order to merge the first and the second columns, my output for the first row shld be ko:K00001/ko:K00003 AAA 1001 (i.e. 370+631) KAA 1811 (i.e. 603+1208) and so on for the whole column. A colleague suggested using rowsum, but that seems to add up all the rows for ALL the columns and not just specified ones. Also, the output doesnt contain the column or row headers. Could you please suggest a solution? Yours sincerely Nabila Binte Zahur
Hi: Based on the information you provided, I would suggest looking into the transform() and within() functions in base R, and perhaps the mutate() function in package plyr. HTH, Dennis On Tue, Jun 21, 2011 at 11:35 PM, Nabila Binte Zahur <nabila at nus.edu.sg> wrote:> Dear Sirs/Madam, > > I am a beginner to R, and I am currently working on a data matrix which looks like this: > >> head(oligo) > > ? ? ? ?ko:K00001 ko:K00003 ko:K00005 ko:K00008 ko:K00009 ko:K00010 ko:K00012 > AAA ? ? ? 370 ? ? ? ? ?631 ? ? ? ? ? ? ? ? ?365 ? ? ? ? ? ? ? 67 ? ? ? ? ? 164 ? ? ? ? ? ? 455 ? ? ? 491 > KAA ? ? ? 603 ? ? ? ? 1208 ? ? ? ? ? ? ? ? 170 ? ? ? ? ? ? ?157 ? ? ? ?68 ? ? ? ? ? ? ? ?495 ? ? ? 922 > NAA ? ? ? ?60 ? ? ? ? 110 ? ? ? ? ? ? ? ? ? ? 10 ? ? ? ? ? ? ? ?7 ? ? ? ? ? ?44 ? ? ? ? ? ? ? 51 ? ? ? ?94 > DAA ? ? ? 183 ? ? ? ? 802 ? ? ? ? ? ? ? ? ? ?41 ? ? ? ? ? ? ? 62 ? ? ? ? ?26 ? ? ? ? ? ? ?166 ? ? ? 263 > CAA ? ? ? ?58 ? ? ? ? ? ?12 ? ? ? ? ? ? ? ? ? ?58 ? ? ? ? ? ? ? ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ?23 ? ? ? ? 8 > TAA ? ? ? 451 ? ? ? ? ?468 ? ? ? ? ? ? ? ? ?201 ? ? ? ? ? ? ? ?90 ? ? ? 131 ? ? ? ? ? ? ?320 ? ? ? 518 > > > For certain specific columns, I need to merge the columns in such a way that the elements get added together. > > For example, in order to merge the first and the second columns, my output for the first row shld be > > ? ? ? ? ? ko:K00001/ko:K00003 > AAA ? ? ? 1001 (i.e. 370+631) > KAA ? ? ? ?1811 (i.e. 603+1208) > > > and so on for the whole column. > > A colleague suggested using rowsum, but that seems to add up all the rows for ALL the columns and not just specified ones. Also, the output doesnt contain the column or row headers. > > Could you please suggest a solution? > > Yours sincerely > Nabila Binte Zahur > > ______________________________________________ > R-help at 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. >
On Wed, Jun 22, 2011 at 2:35 AM, Nabila Binte Zahur <nabila at nus.edu.sg> wrote:> Dear Sirs/Madam, > > I am a beginner to R, and I am currently working on a data matrix which looks like this: > >> head(oligo) > > ? ? ? ?ko:K00001 ko:K00003 ko:K00005 ko:K00008 ko:K00009 ko:K00010 ko:K00012 > AAA ? ? ? 370 ? ? ? ? ?631 ? ? ? ? ? ? ? ? ?365 ? ? ? ? ? ? ? 67 ? ? ? ? ? 164 ? ? ? ? ? ? 455 ? ? ? 491 > KAA ? ? ? 603 ? ? ? ? 1208 ? ? ? ? ? ? ? ? 170 ? ? ? ? ? ? ?157 ? ? ? ?68 ? ? ? ? ? ? ? ?495 ? ? ? 922 > NAA ? ? ? ?60 ? ? ? ? 110 ? ? ? ? ? ? ? ? ? ? 10 ? ? ? ? ? ? ? ?7 ? ? ? ? ? ?44 ? ? ? ? ? ? ? 51 ? ? ? ?94 > DAA ? ? ? 183 ? ? ? ? 802 ? ? ? ? ? ? ? ? ? ?41 ? ? ? ? ? ? ? 62 ? ? ? ? ?26 ? ? ? ? ? ? ?166 ? ? ? 263 > CAA ? ? ? ?58 ? ? ? ? ? ?12 ? ? ? ? ? ? ? ? ? ?58 ? ? ? ? ? ? ? ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ?23 ? ? ? ? 8 > TAA ? ? ? 451 ? ? ? ? ?468 ? ? ? ? ? ? ? ? ?201 ? ? ? ? ? ? ? ?90 ? ? ? 131 ? ? ? ? ? ? ?320 ? ? ? 518 > > > For certain specific columns, I need to merge the columns in such a way that the elements get added together. > > For example, in order to merge the first and the second columns, my output for the first row shld be > > ? ? ? ? ? ko:K00001/ko:K00003 > AAA ? ? ? 1001 (i.e. 370+631) > KAA ? ? ? ?1811 (i.e. 603+1208) > > > and so on for the whole column. >Define a by vector (called collapse here) that has equal numbers for columns to be collapsed and distinct numbers for those that are not to be collapsed. We then transpose, use rowsum and transpose back. Assuming DF is the data frame we have: collapse <- c(1, 1, 3:7) setNames(as.data.frame(t(rowsum(t(DF), collapse))), tapply(names(DF), collapse, paste, collapse = "/")) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
I assume that you are working in package MatrixCalc. Let’s say you have a matrix X. You can call the columns of X this way: X[,1] for the first column X[,2] for the second column X[,3] for the third column Etc. So let’s say that you want to add the first and third column, then you would just do X[,1] + X[,3] If you wanted to call that quantity later on, it might be helpful to give it a name: NewQ <- X[,1] + X[,3] Good luck,Rita On Wed, Jun 22, 2011 at 2:35 AM, Nabila Binte Zahur <nab...@nus.edu.sg> wrote:> Dear Sirs/Madam, > > I am a beginner to R, and I am currently working on a data matrix which looks > like this: > >> head(oligo) > > ko:K00001 ko:K00003 ko:K00005 ko:K00008 ko:K00009 ko:K00010 ko:K00012 > AAA 370 631 365 67 > 164 455 491 > KAA 603 1208 170 157 68 > 495 922 > NAA 60 110 10 7 > 44 51 94 > DAA 183 802 41 62 26 > 166 263 > CAA 58 12 58 1 > 1 23 8 > TAA 451 468 201 90 131 > 320 518 > > > For certain specific columns, I need to merge the columns in such a way that > the elements get added together. > > For example, in order to merge the first and the second columns, my output > for the first row shld be > > ko:K00001/ko:K00003 > AAA 1001 (i.e. 370+631) > KAA 1811 (i.e. 603+1208) > > > and so on for the whole column. >Rita ===================================== "If you think education is expensive, try ignorance."--Derek Bok [[alternative HTML version deleted]]