I have a data frame and one separate vector that is a grouping variable for the data frame. I would like to take all rows of the data frame belonging to each group and then sum the columns with out using a for statement. Something like: take all rows of group 1 then apply(group1.data,1,sum), but do this without having to do it separately for each group. Any ideas? thank you. Dan
?by perhaps --- Daniel O'Shea <dan.oshea at dnr.state.mn.us> wrote:> I have a data frame and one separate vector that is > a grouping variable for the data frame. I would > like to take all rows of the data frame belonging to > each group and then sum the columns with out using a > for statement. > > Something like: take all rows of group 1 then > apply(group1.data,1,sum), but do this without having > to do it separately for each group. Any ideas? > thank you. > > Dan > > ______________________________________________ > 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. >
Here's one way, lapply(split(DF, your.vector), function(x) {apply(x, 2, sum)})> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Daniel O'Shea > Sent: Tuesday, August 21, 2007 3:53 PM > To: r-help at stat.math.ethz.ch > Subject: [R] summing columns of data frame by group > > I have a data frame and one separate vector that is a > grouping variable for the data frame. I would like to take > all rows of the data frame belonging to each group and then > sum the columns with out using a for statement. > > Something like: take all rows of group 1 then > apply(group1.data,1,sum), but do this without having to do it > separately for each group. Any ideas? thank you. > > Dan > > ______________________________________________ > 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. >
Apparently Analagous Threads
- distributing the values of data frame to a vector based on......
- bug report - cor (PR#8852)
- refering to variable names in lm where the variable name is in another variable
- summing values by week - based on daily dates - but with some dates missing
- Summing up columns with NA elements in a data frame