Dear all, I could not find an answer for the problem. I have a matrix and I want to sum rows in groups i.e. rows are summed as long as rows of column 1 have some value (f.e. 2008 in it), then again rows are summed as long as rows of column 2 have some value (f.e. 2009 in it). In case there is a repetition of value in column 1 in another group, this group should be summed seperately. Example: I have such a matrix: *1.2008* 3 2 2 *2.2008* 4 3 3 * 3.2008* 5 4 4 * 4.2008* 3 3 1 *1.2009* 6 1 2 *2.2009* 3 2 5 *1.2010* 4 4 3 * 2.2010* 3 3 2 *3.2010* 1 5 3 *4.2010* 2 2 2 * 1.2008* 3 1 1 *2.2008* 4 3 2 *3.2008* 2 4 3 *1.2009* 1 2 4 *2.2009* 5 4 3 *3.2009* 4 3 3 *1.2010* 4 3 1 *2.2010* 2 2 2 The result should be like this: *2008* 15 12 10 *2009* 9 3 7 *2010* 10 14 10 *2008* 9 8 6 *2009* 10 9 10 *2010* 6 5 3 Could You help me with that? Yours, Andrew Gaska -- View this message in context: http://r.789695.n4.nabble.com/Summing-rows-by-years-each-time-separately-tp4276428p4276428.html Sent from the R help mailing list archive at Nabble.com.
On Jan 8, 2012, at 1:57 PM, Andrew Gaska wrote:> Dear all, > > I could not find an answer for the problem. > > I have a matrix and I want to sum rows in groups i.e. rows are > summed as > long as rows of column 1 have some value (f.e. 2008 in it), then > again rows > are summed as long as rows of column 2 have some value (f.e. 2009 in > it). >?aggregate -- David.> In case there is a repetition of value in column 1 in another group, > this > group should be summed seperately. > > Example: > > I have such a matrix: > > *1.2008* 3 2 2 > *2.2008* 4 3 3 > * 3.2008* 5 4 4 > * 4.2008* 3 3 1 > *1.2009* 6 1 2 > *2.2009* 3 2 5 > *1.2010* 4 4 3 > * 2.2010* 3 3 2 > *3.2010* 1 5 3 > *4.2010* 2 2 2 > * 1.2008* 3 1 1 > *2.2008* 4 3 2 > *3.2008* 2 4 3 > *1.2009* 1 2 4 > *2.2009* 5 4 3 > *3.2009* 4 3 3 > *1.2010* 4 3 1 > *2.2010* 2 2 2 > > The result should be like this: > > *2008* 15 12 10 > *2009* 9 3 7 > *2010* 10 14 10 > *2008* 9 8 6 > *2009* 10 9 10 > *2010* 6 5 3 > > > Could You help me with that? > > Yours, > Andrew Gaska > > > -- > View this message in context: http://r.789695.n4.nabble.com/Summing-rows-by-years-each-time-separately-tp4276428p4276428.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
On Jan 9, 2012, at 9:06 AM, Andrew Gaska wrote:> Thanks for your answer! > > There is one thing I could not find in aggregate(): > > I want to have it a sum for each group of 2008,2009 and 2010. In > aggregate() I can sum all the rows that have a rowname 2008, all > the rows > that have a rowname 2009 and all the rows that have a rowname 2010. > > But I want to have it summed separately each time a new group > starts, so not > like this: > > 2008 24 20 16 > 2009 19 12 17 > 2010 16 19 13 > > but like this: > > 2008 15 12 10 > 2009 9 3 7 > 2010 10 14 10 > 2008 9 8 6 > 2009 10 9 10 > 2010 6 5 3 >You appear to be changing your request. If you want an unambiguous anser submit an unambiguous dataset. Use dput() on hte datafame or matrix and then we can all be on the same page.> > -- > View this message blah blah > Sent from the R help mailing list archive at Nabble.com.We are not reading this on Nabble. Include context. -- David Winsemius, MD West Hartford, CT