n_day: Calculate number of occurrences of a day of the week in a set...

View source: R/n_day.R

n_dayR Documentation

Calculate number of occurrences of a day of the week in a set time period

Description

n_day takes a start date, an end date and a day of the week. It calculates the number of times said day of the week occurs within the given time period.

Usage

n_day(start, end, day)

Arguments

start

The beginning of the time period. Must be a date of Date or POSIXt class.

end

The end of the time period. Must be a date of Date or POSIXt class.

day

The day of the week, entered as a numeric between 0 (Sunday) and 6 (Saturday). It won't accept literal string entries of the day of the week. See day_to_num for converting string entries into the required numeric format.

Details

The Date and POSIXt classes within R treat Sunday as day 0 in a week, and Saturday as day 6. n_day also treats Sunday as day 0 (as opposed to day 7) for the sake of consistency.

See Also

days_of_week for returning a vector of the days of the week.

day_to_num for turning the output from days_of_week into numeric form for use within n_day.

Examples


# Number of Tuesdays in January 2018
n_day(lubridate::dmy(01012018), lubridate::dmy(31012018), 2)

# Number of Thursdays in 2019
n_day(lubridate::dmy(01012019), lubridate::dmy(31122019), day_to_num("Thu"))

# Number of weekdays (Monday-Friday) in 2020
library(magrittr)
purrr::map_dbl(1:5, ~ n_day(lubridate::dmy(01012020), 
                            lubridate::dmy(31122020), 
                            .x)) %>% sum()


jackhannah95/jafun documentation built on Feb. 9, 2023, 12:24 p.m.