R: Round down dates to first day of the week

前端 未结 3 2045
抹茶落季
抹茶落季 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:12

    With lubridate you could try this:

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

    floor_date starts weeks on Sundays, so to avoid those being included in the next week you have to subtract one before rounding and then increase the value by one day.

提交回复
热议问题