Dear R members, I have this data frame of 100 years in the following format year month day A B C D where A,B,C and D are item number sold each day. I am trying 1-split the data w.r.t the monthly values for each year 2-then, sum them up I am pasting here just a part of data to make it more clearer structure(list(year = c(1961, 1961, 1961, 1961, 1961, 1961, 1961, 1961, 1961, 1961, 1961, 1961), month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), day = 1:12, A = 1:12, B = 3:14, C = 6:17, D = 16:27), .Names = c("year", "month", "day", "A", "B", "C", "D"), row.names = c(NA, 12L), class = "data.frame") I initially tried to use "dcast" command but for no use. Your kind help is needed. Thanks in advance Eliza [[alternative HTML version deleted]]
?tapply e.g. with(yourdata, tapply(A,list(year,month),sum,simplify=FALSE)) This assumes "sum them up" means summing each column separately. You were unclear as to exactly what you meant by this. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Mon, Sep 8, 2014 at 12:08 PM, eliza botto <eliza_botto at hotmail.com> wrote:> Dear R members, > > I have this data frame of 100 years in the following format > > year month day A B C D > > where A,B,C and D are item number sold each day. I am trying > > 1-split the data w.r.t the monthly values for each year > > 2-then, sum them up > > I am pasting here just a part of data to make it more clearer > > structure(list(year = c(1961, 1961, 1961, 1961, 1961, 1961, 1961, > 1961, 1961, 1961, 1961, 1961), month = c(1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1), day = 1:12, A = 1:12, B = 3:14, C = 6:17, D = 16:27), .Names = c("year", > "month", "day", "A", "B", "C", "D"), row.names = c(NA, 12L), class = "data.frame") > > I initially tried to use "dcast" command but for no use. > > Your kind help is needed. > > Thanks in advance > > Eliza > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 Mon, Sep 8, 2014 at 2:08 PM, eliza botto <eliza_botto at hotmail.com> wrote:> Dear R members, > > I have this data frame of 100 years in the following format > > year month day A B C D > > where A,B,C and D are item number sold each day. I am trying > > 1-split the data w.r.t the monthly values for each year > > 2-then, sum them up > > I am pasting here just a part of data to make it more clearer > > structure(list(year = c(1961, 1961, 1961, 1961, 1961, 1961, 1961, > 1961, 1961, 1961, 1961, 1961), month = c(1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1), day = 1:12, A = 1:12, B = 3:14, C = 6:17, D = 16:27), .Names = c("year", > "month", "day", "A", "B", "C", "D"), row.names = c(NA, 12L), class = "data.frame") > > I initially tried to use "dcast" command but for no use. > > Your kind help is needed. > > Thanks in advance > > ElizaI'm not sure if I really understand what you want, but perhaps this? library(dplyr); summarize(group_by(data,year,month),sum(A),sum(B),sum(C),sum(D)); If you are SQL oriented this is equivalent to the SQL query: select year, month, sum(A), sum(B), sum(C), sum(D) from data group by year, month ; -- There is nothing more pleasant than traveling and meeting new people! Genghis Khan Maranatha! <>< John McKown
On Sep 8, 2014, at 12:08 PM, eliza botto wrote:> Dear R members, > > I have this data frame of 100 years in the following format > > year month day A B C D > > where A,B,C and D are item number sold each day. I am trying > > 1-split the data w.r.t the monthly values for each year > > 2-then, sum them up > > I am pasting here just a part of data to make it more clearer > > structure(list(year = c(1961, 1961, 1961, 1961, 1961, 1961, 1961, > 1961, 1961, 1961, 1961, 1961), month = c(1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1), day = 1:12, A = 1:12, B = 3:14, C = 6:17, D = 16:27), .Names = c("year", > "month", "day", "A", "B", "C", "D"), row.names = c(NA, 12L), class = "data.frame") > > I initially tried to use "dcast" command but for no use. >This is typical use for base function aggregate:> with( dfrm, aggregate(dfrm[4:7], dfrm[1:2], FUN=sum))year month A B C D 1 1961 1 78 102 138 258> > [[alternative HTML version deleted]]Please stop posting in plain text. (You have already been asked multiple times.) -- David Winsemius Alameda, CA, USA