dplyr, lubridate : how to aggregate a dataframe by week?

后端 未结 4 941
时光取名叫无心
时光取名叫无心 2020-11-29 03:54

Consider the following example

library(tidyverse)
library(lubridate)
time <- seq(from =ymd(\"2014-02-24\"),to= ymd(\"2014-03-20\"), by=\"days\")
set.seed(         


        
4条回答
  •  醉梦人生
    2020-11-29 04:10

    Just this once, after some research, I actually think I came up with a better solution that

    • gives the correct aggregation
    • gives the correct labels

    Example below for weeks starting on a thursday. The weeks will be labeled by their first day a given cycle.

    library(tidyverse)
    library(lubridate)
    options(tibble.print_min = 30)
    
    time <- seq(from =ymd("2014-02-24"),to= ymd("2014-03-20"), by="days")
    set.seed(123)
    values <- sample(seq(from = 20, to = 50, by = 5), size = length(time), replace = TRUE)
    df2 <- data_frame(time, values)
    
    df2 <- df2 %>% mutate(day_of_week_label = wday(time, label = TRUE),
                          day_of_week = wday(time, label = FALSE))
    
    df2 <- df2 %>% mutate(thursday_cycle = time - ((as.integer(day_of_week) - 5) %% 7),
                          tmp_1 = (as.integer(day_of_week) - 5),
                          tmp_2 = ((as.integer(day_of_week) - 5) %% 7))
    

    which gives

    > df2
    # A tibble: 25 × 7
             time values day_of_week_label day_of_week thursday_cycle tmp_1 tmp_2
                                            
    1  2014-02-24     30               Mon           2     2014-02-20    -3     4
    2  2014-02-25     45              Tues           3     2014-02-20    -2     5
    3  2014-02-26     30               Wed           4     2014-02-20    -1     6
    4  2014-02-27     50             Thurs           5     2014-02-27     0     0
    5  2014-02-28     50               Fri           6     2014-02-27     1     1
    6  2014-03-01     20               Sat           7     2014-02-27     2     2
    7  2014-03-02     35               Sun           1     2014-02-27    -4     3
    8  2014-03-03     50               Mon           2     2014-02-27    -3     4
    9  2014-03-04     35              Tues           3     2014-02-27    -2     5
    10 2014-03-05     35               Wed           4     2014-02-27    -1     6
    11 2014-03-06     50             Thurs           5     2014-03-06     0     0
    12 2014-03-07     35               Fri           6     2014-03-06     1     1
    13 2014-03-08     40               Sat           7     2014-03-06     2     2
    14 2014-03-09     40               Sun           1     2014-03-06    -4     3
    15 2014-03-10     20               Mon           2     2014-03-06    -3     4
    16 2014-03-11     50              Tues           3     2014-03-06    -2     5
    17 2014-03-12     25               Wed           4     2014-03-06    -1     6
    18 2014-03-13     20             Thurs           5     2014-03-13     0     0
    19 2014-03-14     30               Fri           6     2014-03-13     1     1
    20 2014-03-15     50               Sat           7     2014-03-13     2     2
    21 2014-03-16     50               Sun           1     2014-03-13    -4     3
    22 2014-03-17     40               Mon           2     2014-03-13    -3     4
    23 2014-03-18     40              Tues           3     2014-03-13    -2     5
    24 2014-03-19     50               Wed           4     2014-03-13    -1     6
    25 2014-03-20     40             Thurs           5     2014-03-20     0     0
    

提交回复
热议问题