I have two data frames. One with a matrix of months and the other with a matrix of values. The two dataframes correspond to each other. I would like to sum up all the values in by month. What would be an efficient way to do this? a=C(2,3,5,2,3,5) b=c(2,6,3,2,6,3) c=c(2,6,7,2,6,5) months <- data.frame(a,b,c) a=C(200,345,5435,27456,3343,555) b=c(288,699,377,234,6354,33454) c=c(2345,6345,79678,245,676,5089) values <- data.frame(a,b,c) -- View this message in context: http://r.789695.n4.nabble.com/conditional-sum-two-dataframes-tp4637986.html Sent from the R help mailing list archive at Nabble.com.
The values need not be a data frame as the number of unique months could be different among the columns, right?. So you're going to have to rethink your data structure. Probably as a list. Once you get that straight,?tapply and friends should make it trivial, if I understand you correctly. -- Bert On Thu, Jul 26, 2012 at 10:49 AM, jcrosbie <james at crosb.ie> wrote:> I have two data frames. One with a matrix of months and the other with a > matrix of values. The two dataframes correspond to each other. I would like > to sum up all the values in by month. > > What would be an efficient way to do this? > > a=C(2,3,5,2,3,5) > b=c(2,6,3,2,6,3) > c=c(2,6,7,2,6,5) > > months <- data.frame(a,b,c) > > a=C(200,345,5435,27456,3343,555) > b=c(288,699,377,234,6354,33454) > c=c(2345,6345,79678,245,676,5089) > > values <- data.frame(a,b,c) > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/conditional-sum-two-dataframes-tp4637986.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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hello, Try the following. m <- unlist(months) v <- unlist(values) aggregate(v ~ m, FUN=sum) tapply(v, m, sum) Hope this helps, Rui Barradas Em 26-07-2012 18:49, jcrosbie escreveu:> I have two data frames. One with a matrix of months and the other with a > matrix of values. The two dataframes correspond to each other. I would like > to sum up all the values in by month. > > What would be an efficient way to do this? > > a=C(2,3,5,2,3,5) > b=c(2,6,3,2,6,3) > c=c(2,6,7,2,6,5) > > months <- data.frame(a,b,c) > > a=C(200,345,5435,27456,3343,555) > b=c(288,699,377,234,6354,33454) > c=c(2345,6345,79678,245,676,5089) > > values <- data.frame(a,b,c) > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/conditional-sum-two-dataframes-tp4637986.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.