Hello,
floor_date(x, unit = 'weeks')
returns the same value (the previous Sunday, in my testing) regardless of what the lubridate.week.start
option is set to:
library(lubridate)
options(lubridate.week.start = 7)
wday(ymd('2017-01-02')) # 2
floor_date(ymd('2017-01-02'), unit = 'weeks') # "2017-01-01"
options(lubridate.week.start = 1)
wday(ymd('2017-01-02')) # 1
floor_date(ymd('2017-01-02'), unit = 'weeks') # "2017-01-01"
As the above snippet shows, even though the results of wday(x)
change according to lubridate.week.start
, the results of floor_date(x, unit = 'weeks')
do not. Intuitively, if the week start is set to Sunday, then floor_date()
should always return a Sunday; if the option is set to Monday, then the function should always return a Monday, and so on.
I believe the issue is actually due to the update()
function also ignoring this option:
options(lubridate.week.start = 7)
update(ymd('2017-01-02'), wday = 1) # "2017-01-01"
wday(ymd('2017-01-01')) # 1
options(lubridate.week.start = 1)
update(ymd('2017-01-02'), wday = 1) # "2017-01-01"
wday(ymd('2017-01-01')) # 7
In the latter case, wday(update(x, wday = 1))
does not give a result of 1
, even though we explicitly set wday = 1
.
ceiling_date also doesn't use the week.start option
for most work I need the week to end on a friday and have resorted to a case_when to get there.
@pjrdata I have a quick hack of a solution here which you may find useful:
floor_date_by_week <- function(the_date) {
return(date(the_date) - wday(the_date + 1) + 1)
}
ceiling_date_by_week <- function(the_date) {
return(date(the_date) - wday(the_date) + 7)
}
Example:
````
for (week_start in seq(1, 7)) { # 1 = Sun, 7 = Sat
options(lubridate.week.start = week_start)
print(floor_date_by_week(ymd('2017-03-16')))
}
[1] "2017-03-12"
[1] "2017-03-13"
[1] "2017-03-14"
[1] "2017-03-15"
[1] "2017-03-16"
[1] "2017-03-10"
[1] "2017-03-11"
for (week_start in seq(1, 7)) { # 1 = Sun, 7 = Sat
options(lubridate.week.start = week_start)
print(ceiling_date_by_week(ymd('2017-03-16')))
}
[1] "2017-03-19"
[1] "2017-03-20"
[1] "2017-03-21"
[1] "2017-03-22"
[1] "2017-03-16"
[1] "2017-03-17"
[1] "2017-03-18"
````
I haven't fully tested this, so my apologies if there's a mistake!
Fixed. Thanks for reporting.
Hi, I'm still seeing this issue with version 1.6.0
1.7.0 should be on CRAN within a day or two.
Hi it seems this issue has resurfaced. I'm using lubridate 1.7.4
round_date(ymd("2019-10-22"),unit="week",week_start = 5)
[1] "2019-10-20"
round_date(ymd("2019-10-22"),unit="week",week_start = 1)
[1] "2019-10-20"
Here's the session info in case there is any namespace issues.
sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.7.4
loaded via a namespace (and not attached):
[1] Rcpp_0.12.18 crayon_1.3.4 dplyr_0.7.6 assertthat_0.2.0 R6_2.2.2 magrittr_1.5 pillar_1.3.0 stringi_1.2.4
[9] rlang_0.2.2 rstudioapi_0.7 bindrcpp_0.2.2 tools_3.5.1 stringr_1.3.1 glue_1.3.0 purrr_0.2.5 yaml_2.2.0
[17] compiler_3.5.1 pkgconfig_2.0.2 bindr_0.1.1 knitr_1.20 tidyselect_0.2.4 tibble_1.4.2
Please use development. The OP was about floor_date, there was a bug in round_date as well.
Most helpful comment
Fixed. Thanks for reporting.