tidyverse / lubridate

Make working with dates in R just that little bit easier
https://lubridate.tidyverse.org
GNU General Public License v3.0
728 stars 207 forks source link

ISOWEEK and ISOYEAR options for Floor/Ceil/Round #293

Open datalove opened 9 years ago

datalove commented 9 years ago

Now that we have both isoyear and isoweek, it would be good to include them as options for floor_date, ceil_date and round_date.

For example: floor(ymd('2016-01-02'),'isoweek') is 2015-12-28 floor(ymd('2016-01-02'),'isoyear') is 2014-12-29

Will need to create assignment functions for isoweek<- and isoyear<-.

Happy to work on this if it's a good idea!

vspinu commented 9 years ago

Happy to work on this if it's a good idea!

Yes. Of course. Thanks.

vspinu commented 9 years ago

Related #268

DavisVaughan commented 3 years ago

You can do this with clock's iso-year-week-day calendar type

library(clock)
library(magrittr)

ywd <- year_month_day(2016, 1, 2) %>%
  as_iso_year_week_day()

ywd
#> <iso_year_week_day<day>[1]>
#> [1] "2015-W53-6"

# Grouping by the ISO week component
ywd %>%
  calendar_group("week", n = 1) %>%
  calendar_widen("day") %>%
  as.Date()
#> [1] "2015-12-28"

# Grouping by the ISO year component
ywd %>%
  calendar_group("year", n = 1) %>%
  calendar_widen("day") %>%
  as.Date()
#> [1] "2014-12-29"

# Setting components
set_week(ywd, 2)
#> <iso_year_week_day<day>[1]>
#> [1] "2015-W02-6"
set_year(ywd, 2017)
#> <iso_year_week_day<day>[1]>
#> [1] "2017-W53-6"

Created on 2021-05-25 by the reprex package (v1.0.0)

https://clock.r-lib.org/reference/index.html#section-iso-year-week-day