r-lib / clock

A Date-Time Library for R
https://clock.r-lib.org
Other
102 stars 5 forks source link

Add TRUE/FALSE abbreviation argument to `weekday()` #316

Closed batpigandme closed 1 year ago

batpigandme commented 1 year ago

Most of the time we want weekdays to be abbreviated, but it'd be nice if there was an argument for as_weekday() and/or weekday() that allowed you to print the full name of the day, like with abbr in lubridate::wday(), especially since the full name of weekdays are used with clock_weekdays (at least in the examples).

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
today <- today()
today
#> [1] "2023-04-03"
clock::as_weekday(today)
#> <weekday[1]>
#> [1] Mon
wday(today, label = TRUE)
#> [1] Mon
#> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

# It'd be nice to have an equivalent of this in `clock::as_weekday()`
wday(today, label = TRUE, abbr = FALSE)
#> [1] Monday
#> 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Created on 2023-04-03 with reprex v2.0.2 I think the argument, abbreviate, is already there in the underlying functions, e.g. https://github.com/r-lib/clock/blob/64c1a53a7df0b3f4c04555a03a6472450f8745ce/R/weekday.R#L121 but it can't be sent into as_weekday().

batpigandme commented 1 year ago

Counter argument to myself. This is already there in weekday_factor(), so maybe it's not necessary.

weekdays <- clock::weekday(1:7)
clock::weekday_factor(weekdays)
#> [1] Sun Mon Tue Wed Thu Fri Sat
#> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
clock::weekday_factor(weekdays, abbreviate = FALSE)
#> [1] Sunday    Monday    Tuesday   Wednesday Thursday  Friday    Saturday 
#> 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Created on 2023-04-03 with reprex v2.0.2

DavisVaughan commented 1 year ago

Yea and I think most users should use the high-level API function, date_weekday_factor(), where the input is Date and the output is an ordered factor (i.e. base R types only)

library(clock)

x <- as.Date("2023-05-01") + 0:3

date_weekday_factor(x)
#> [1] Mon Tue Wed Thu
#> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

Most of the time the high-level API should be sufficient, i.e. anything with date_*()

batpigandme commented 1 year ago

Yeah, makes sense. IDK how I got sidetracked into weekday() world.