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

Possible improvements to 'wday.numeric' #1026

Closed jaganmn closed 1 year ago

jaganmn commented 2 years ago

Re: #1025

wday.numeric gains an optional argument week_start_x indicating the coding of argument x. The default value is 7, meaning 1=Sunday, for backwards compatibility. Hence anyone doing wday(<numeric>) without specifying week_start_x should see the same behaviour as before.

Some tests, which could easily go into test-accessors.R ...

library("lubridate")

wday(1:7, week_start_x = 7, week_start = 7) # from 1=Sunday to 1=Sunday (no-op)
## [1] 1 2 3 4 5 6 7

wday(1:7, week_start_x = 1, week_start = 7) # from 1=Monday to 1=Sunday
## [1] 2 3 4 5 6 7 1

wday(1:7, week_start_x = 7, week_start = 1) # from 1=Sunday to 1=Monday
## [1] 7 1 2 3 4 5 6
jaganmn commented 2 years ago

An even nicer (and still backwards compatible) interface might be:

library("lubridate")

wday(1:7, from_sunday = 1, to_sunday = 1)
## [1] 1 2 3 4 5 6 7

wday(1:7, from_sunday = 7, to_sunday = 1)
## [1] 2 3 4 5 6 7 1

wday(1:7, from_sunday = 1, to_sunday = 7)
## [1] 7 1 2 3 4 5 6

The default value of from_sunday is 1 and the default value of to_sunday is 8 - week_start, so again anyone doing wday(<numeric>) without specifying any of the new, optional arguments should see the same behaviour as before.

The same tests with label = TRUE:

wday(1:7, from_sunday = 1, to_sunday = 1, label = TRUE)
## [1] Sun Mon Tue Wed Thu Fri Sat
## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

wday(1:7, from_sunday = 7, to_sunday = 1, label = TRUE)
## [1] Mon Tue Wed Thu Fri Sat Sun
## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

wday(1:7, from_sunday = 1, to_sunday = 7, label = TRUE)
## [1] Sun Mon Tue Wed Thu Fri Sat
## Levels: Mon < Tue < Wed < Thu < Fri < Sat < Sun