R: Round down dates to first day of the week

前端 未结 3 2055
抹茶落季
抹茶落季 2021-01-04 20:37

I have a dataframe where one of the columns contains dates (some dates appear multiple times). I want to aggregate the dates by week. The best way I can think of this is to

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 21:29

    With the week_startparameter in the floor_date function of the lubridate package you have the option to specify the beginning of the week since lubridate version 1.7.0. This allows you to perform:

    library(lubridate)
    dates <- seq.Date(as.Date("2016-04-04"), as.Date("2016-04-14"), by = 1)
    floor_date(dates, "weeks", week_start = 1)
    

    I would post it as a comment to Sraffa's response but I don't have the reputation.

提交回复
热议问题