Aggregate data to weekly level with every week starting from Monday

后端 未结 3 607
小鲜肉
小鲜肉 2021-01-18 07:35

I have a data frame like,

2015-01-30     1       Fri
2015-01-30     2       Sat
2015-02-01     3       Sun
2015-02-02     1       Mon
2015-02-03     1                


        
3条回答
  •  感情败类
    2021-01-18 08:26

    This should work. I've called the dataframe m and named the columns possibly different to yours.

    library(plyr) # install.packages("plyr")
    
    colnames(m) = c("Date", "count","Day")
    start  = as.Date("2015-01-26")
    m$Week <- floor(unclass(as.Date(m$Date) - as.Date(start)) / 7) + 1
    m$Week = as.numeric(m$Week)
    m %>% group_by(Week) %>% summarise(count = sum(count))
    

    The library plyr is great for data manipulation, but it's just a rough hack to get the week number in.

提交回复
热议问题