New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

floor_date(x, unit = 'weeks') ignores lubridate.week.start option #509

Closed
MichaelJW opened this Issue Jan 18, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@MichaelJW

MichaelJW commented Jan 18, 2017

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.

@pjrdata

This comment has been minimized.

Show comment
Hide comment
@pjrdata

pjrdata Mar 16, 2017

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 commented Mar 16, 2017

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.

@MichaelJW

This comment has been minimized.

Show comment
Hide comment
@MichaelJW

MichaelJW Mar 16, 2017

@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!

MichaelJW commented Mar 16, 2017

@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!

@vspinu

This comment has been minimized.

Show comment
Hide comment
@vspinu

vspinu May 7, 2017

Member

Fixed. Thanks for reporting.

Member

vspinu commented May 7, 2017

Fixed. Thanks for reporting.

@vspinu vspinu closed this May 7, 2017

@amjadtalib

This comment has been minimized.

Show comment
Hide comment
@amjadtalib

amjadtalib Oct 9, 2017

Hi, I'm still seeing this issue with version 1.6.0

amjadtalib commented Oct 9, 2017

Hi, I'm still seeing this issue with version 1.6.0

@vspinu

This comment has been minimized.

Show comment
Hide comment
@vspinu

vspinu Oct 9, 2017

Member

1.7.0 should be on CRAN within a day or two.

Member

vspinu commented Oct 9, 2017

1.7.0 should be on CRAN within a day or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment