Aggregate data to weekly level with every week starting from Monday

后端 未结 3 633
小鲜肉
小鲜肉 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:19

    Wit dplyr and lubridate is this really easy thanks to the function isoweek

    my.df <- read.table(header=FALSE, text=
      '2015-01-30     1       Fri
       2015-01-30     2       Sat
       2015-02-01     3       Sun
       2015-02-02     1       Mon
       2015-02-03     1       Tue
       2015-02-04     1       Wed 
       2015-02-05     1       Thu
       2015-02-06     1       Fri
       2015-02-07     1       Sat
       2015-02-08     1       Sun')
     my.df %>% mutate(week = isoweek(V1)) %>% group_by(week) %>% summarise(sum(V2))
    

    or a bit shorter

    my.df %>% group_by(isoweek(V1)) %>% summarise(sum(V2))
    

提交回复
热议问题