expand_date function

Generate Temporal Identifiers from a Date Column

Generate Temporal Identifiers from a Date Column

This function expands a date column and generates additional temporal identifiers from it (such as the year, month, quarter, fiscal year etc.).

expand_date( x, gen = c("Year", "Quarter", "FY", "QFY", "Month"), origin = "1899-12-30", keep.date = TRUE, remove.missing.date = TRUE, sort = TRUE, as.factor = TRUE, ... )

Arguments

  • x: either a vector of class 'Date', or coercible to date using as.Date, or a data frame / list with a date variable in the first column.
  • gen: character. A vector of identifiers to generate from x. The possible identifiers are found in .Tvars. The default setting is to generate all identifiers apart from "Day".
  • origin: character / Date. Passed to as.Date: for converting numeric x to date.
  • keep.date: logical. TRUE will keep the date variable in the first column of 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, the corresponding rows will be removed.
  • sort: logical. TRUE will sort the data by the date column.
  • as.factor: TRUE will generate quarters, fiscal years and months ('Quarter', 'FY', 'QFY', 'Month') as factor variables. It is also possible to use as.factor = "ordered" to generate ordered factors. FALSE will generate fiscal years as character and quarters and months as integer variables.
  • ...: not used.

Returns

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

Examples

# First a basic example x <- seq.Date(as.Date("1999-01-01"), as.Date("2000-01-01"), by = "month") expand_date(x) expand_date(x, gen = c("Year", "Month"), keep.date = FALSE) # Now using the API expand_date(get_data("BOU_CPI")) # Getting Monthly CPI data from the Bank of Uganda # Same thing get_data("BOU_CPI", expand.date = TRUE)

See Also

make_date, ugatsdb