f.daily.in.week: Create a 'Daily-In-Week' Frequency

View source: R/frequency.R

f.daily.in.weekR Documentation

Create a Daily-In-Week Frequency


Use this function to create a frequency for time-series data that occurs daily within a subset of a week. The first day of the interval is used as the reference.


f.daily.in.week(date, weekStart = "mon", weekEnd = "fri", forward = TRUE)



The date, which can be a list with year, month, and day elements. It can also be an integer array with 3 elements for year, month, and day respectively, or an object that can be used as an argument for the base::as.Date function.


The first day of the week, which can be sun, mon, tue, wed, thu, fri, or sat.


The last day of the week, which can be one of the values listed for weekStart. Together, they define the week.


If the current date is not in the week and this value is true, it moves forward to the first day of the week. If this value is false, it moves backward to the last day of the week.


In order to use the as.frequency function for this type of frequency, you need the following information:

  • Character Format: The first day of the interval in "YYYYMMDD" format.

  • Class Id: "i:...-..." (where the first '...' represents weekStart and the second '...' represents weekEnd; e.g., i:mon-fri means a week from Monday to Friday)


An object of class ldtf. It is also a list with the following members:


Determines the class of this frequency.


Determines the year.


Determines the month.


Determines the day.


Determines the weekStart.


Determines the weekEnd.


dw0 <- f.daily.in.week(c(2023, 5, 16), "mon", "fri") # This is 16/5/2023.
dw0_value_str <-  as.character(dw0) # this will be '20230516'.
dw0_class_str <- get.class.id(dw0) # this will be 'i:mon-fri'.

# Let's use the same date with another week definition:
dw1 <- f.daily.in.week(c(2023, 5, 16), "wed", "sat")
#     This is NOT 16/5/2023. It is 17/5/2023.
#     Since it was outside the week, we moved it forward.
dw2 <- f.daily.in.week(c(2023, 5, 16), "wed", "sat", FALSE)
#     This is 13/5/2023. The original day was outside the
#     week, but we moved backward too the end of
#     the previous week (which is Saturday).

dw_new <- as.frequency("20230519", "i:sat-wed")
#     This is 20/1/2023 (by default, it moves forward).

# Don't use invalid or unsupported dates:

dw_invalid <- try(as.frequency("1399109", "d3")) # this is a too old date and unsupported
dw_invalid <- try(as.frequency("20230132", "d4")) # invalid day in month
dw_invalid <- try(as.frequency("20231331", "d5")) # invalid month

# don't use invalid week definitions:
dw_invalid <- try(f.daily.in.week(c(2023, 5, 16), "Wednesday", "sat"))

tdata documentation built on Nov. 7, 2023, 5:07 p.m.