Collapse daily data frame down to weekly data frame in R -
this question has answer here:
i have data frame follows:
office string_date b c d e 1: 11 2010-06-01 0 0 1 0 0 2: 11 2010-06-02 0 0 0 1 0 3: 11 2010-06-03 0 0 0 1 0 4: 11 2010-06-04 0 0 0 1 0 5: 11 2010-06-05 0 0 0 1 0 6: 11 2010-06-06 0 1 0 0 0 7: 11 2010-06-07 0 1 0 0 0 8: 11 2010-06-08 0 1 0 0 0 9: 11 2010-06-09 0 1 0 0 0 10: 11 2010-06-10 0 1 0 0 0 11: 11 2010-06-11 0 1 0 0 0 12: 11 2010-06-12 0 1 0 0 0
i need aggregate sums of each column week, not day listed. left each week each office, sums of each column. first line be...
office week b c d e 11 1 0 2 1 4 0 11 2 .............
i using like:
agg <- aggregate(list(data$a, data$b, data$c, data$d, data$e), list(office = data$office, date = data$string_date), sum)
but of course doesn't have ability collapse dates down week. also, these dates strings, not posix.
create new column called week
using week
function , split-apply-combine on that:
dt[,week := week(string_date)][, lapply(.sd, sum), = "office,week", .sdcols = 3:6]
or in dplyr
:
library(dplyr) dt %>% group_by(office, week = week(string_date)) %>% summarise_each(funs(sum), -string_date)
Comments
Post a Comment