
Extract timeframe values from datetime vectors
Source:R/timeframes.R, R/timeslices.R
as_timeframe.RdExtracts a single timeframe component (YEAR, MONTH, HOUR, ...) from a
Date, POSIXct, or POSIXlt vector, optionally formatted as a token
string.
Usage
as_timeframe(x, timeframe, format = c("numeric", "token"), ...)
# S3 method for class 'POSIXt'
as_timeframe(x, timeframe, format = c("numeric", "token"), ...)
# S3 method for class 'Date'
as_timeframe(x, timeframe, format = c("numeric", "token"), ...)
# S3 method for class 'numeric'
as_timeframe(
x,
timeframe,
format = c("numeric", "token"),
calendar = NULL,
...
)
# Default S3 method
as_timeframe(x, timeframe, format = c("numeric", "token"), ...)
# S3 method for class 'character'
as_timeframe(
x,
timeframe,
format = c("numeric", "token"),
calendar = NULL,
year = NULL,
...
)Arguments
- x
A datetime vector (
Date,POSIXct, orPOSIXlt).- timeframe
Character scalar naming the timeframe to extract; one of
CORE_TIMEFRAMES.- format
"numeric"(default) for raw integer values, or"token"for formatted strings ("m01","h00","d365","MON", ...).- ...
Currently
week_startis forwarded tolubridate::wday()forWDAYandMWEEK. Token vocabularies such aswd7are ISO (Monday first), so pair them withweek_start = 1; the default is lubridate's (Sunday first) and differs from those vocabularies by one.- calendar
A
Calendar, or the name of one ("d365_h24"). Required for character input: the timeframe is read from the calendar rather than parsed out of the label. For numeric input it is optional, and selects the calendar's own label vocabulary over the canonical one.- year
The year the timeslices fall in. Needed only when
timeframeis weekday-derived (WDAY,MWEEK,DAYTYPE,WHOUR);MONTH,YDAY,QUARTER,SEASON,WEEKandMDAYare the same in every year.
Value
Integer/numeric vector for format = "numeric", character vector
for format = "token". Preserves NA positions.
Details
For datetime input (no calendar), lubridate defaults apply:
WDAYusesgetOption("lubridate.week.start", 7)(1 = Sunday).MONTH: 1 = January, 12 = December.HOUR: 0-23.MWEEK: calendar-grid week-of-month aligned toWDAY.