Hello, I am trying to figure out how to do a sum of items within a row. For example, I have a data frame something like this: A1 B1 ... A2 B2 ... 1 1 4 1 4 2 2 5 2 5 3 3 6 3 6 What I want, is for each row, to get A1 + A2, B1 + B2, etc. which would, perhaps, give me a vector something like this c(2, 8, ... 4, 10 ... 6, 12). Does anyone know of a simple way to do this? Thanks a lot, Q -- View this message in context: http://r.789695.n4.nabble.com/summing-items-within-a-row-tp2195458p2195458.html Sent from the R help mailing list archive at Nabble.com.
On May 11, 2010, at 8:36 PM, Q wrote:> > Hello, > > I am trying to figure out how to do a sum of items within a row. For > example, I have a data frame something like this: > > A1 B1 ... A2 B2 ... > 1 1 4 1 4 > 2 2 5 2 5 > 3 3 6 3 6 > > What I want, is for each row, to get A1 + A2, B1 + B2, etc. which > would, > perhaps, give me a vector something like this c(2, 8, ... 4, 10 ... > 6, 12).Addition of matrices with the "+" operator should give you a matrix which would have the corresponding sums. Because matrices are really vectors that are read in column order you might need to transpose before you read them out with c(). As always properly formed examples would be appreciated and the form of the appreciation would probably take the form of tested code. -- David. --> > Does anyone know of a simple way to do this? > > Thanks a lot, > > Q > > -- > View this message in context: http://r.789695.n4.nabble.com/summing-items-within-a-row-tp2195458p2195458.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Hi: Does the following work? xx <- matrix(1:5, nrow = 4, ncol = 10)> colnames(xx) <- paste(rep(LETTERS[1:5], 2), rep(1:2, each = 5), sep = '') > xxA1 B1 C1 D1 E1 A2 B2 C2 D2 E2 [1,] 1 5 4 3 2 1 5 4 3 2 [2,] 2 1 5 4 3 2 1 5 4 3 [3,] 3 2 1 5 4 3 2 1 5 4 [4,] 4 3 2 1 5 4 3 2 1 5 # Reshape into a two-column matrix (20 x 2, filled columnwise) and # take the row sums> rowSums(matrix(xx, ncol = 2))[1] 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 This of course assumes that the names in your columns are in parallel subgroups (A*, B*, ..., E* in this toy example). HTH, Dennis On Tue, May 11, 2010 at 5:36 PM, Q <quagaars@gmail.com> wrote:> > Hello, > > I am trying to figure out how to do a sum of items within a row. For > example, I have a data frame something like this: > > A1 B1 ... A2 B2 ... > 1 1 4 1 4 > 2 2 5 2 5 > 3 3 6 3 6 > > What I want, is for each row, to get A1 + A2, B1 + B2, etc. which would, > perhaps, give me a vector something like this c(2, 8, ... 4, 10 ... 6, 12). > > Does anyone know of a simple way to do this? > > Thanks a lot, > > Q > > -- > View this message in context: > http://r.789695.n4.nabble.com/summing-items-within-a-row-tp2195458p2195458.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
Thank you Dennis. That was a great idea and it worked quite well! -- View this message in context: http://r.789695.n4.nabble.com/summing-items-within-a-row-tp2195458p2196793.html Sent from the R help mailing list archive at Nabble.com.
Seemingly Similar Threads
- How to calc ratios base on current and previous row?
- matlab norm(h) command in R: sqrt(sum(h^2)) - use in an expression
- Contrast matrices for nested factors
- Interpreting model matrix columns when using contr.sum
- splitting character vectors into multiple vectors using strsplit