sm_expand_date {samadb}R Documentation

Generate Temporal Identifiers from a Date Column

Description

This function expands a date column and generates additional temporal identifiers from it (year, month, quarter, day).

Usage

sm_expand_date(
  x,
  gen = c("year", "quarter", "month"),
  origin = "1899-12-30",
  keep.date = TRUE,
  remove.missing.date = TRUE,
  sort = TRUE,
  as.factor = TRUE,
  name = "date",
  ...
)

Arguments

x

either a vector of class 'Date', or coercible to date using as.Date, or a data frame / list containing with a date-column called name.

gen

character. A vector of identifiers to generate from x. The possible identifiers are found in .SAMADB_T.

origin

character / Date. Passed to as.Date: for converting numeric x to date. The default reflects converting date-numbers from Excel for Windows.

keep.date

logical. TRUE will keep the date variable in the resulting dataset, FALSE will remove the date variable in favor of the generated identifiers.

remove.missing.date

logical. TRUE will remove missing values in x. If x is a dataset, rows missing the date variable will be removed.

sort

logical. TRUE will sort the data by the date column.

as.factor

TRUE will generate quarters and months as factor variables. It is also possible to use as.factor = "ordered" to generate ordered factors. FALSE will generate quarters and months as integer variables.

name

character. The name of the date variable to expand.

...

not used.

Value

A data.table containing the computed identifiers as columns. See Examples.

See Also

sm_as_date, samadb

Examples

# First a basic example
x <- seq.Date(as.Date("1999-01-01"), as.Date("2000-01-01"), by = "month")
sm_expand_date(x)
sm_expand_date(x, gen = .SAMADB_T[-1L], keep.date = FALSE)

# Now using the API
sm_expand_date(sm_data("BUSINESS_CYCLES"))

# Same thing
sm_data("BUSINESS_CYCLES", expand.date = TRUE)



[Package samadb version 0.2.6 Index]