date_factor {mltools}R Documentation

Date Factor

Description

Map a vector of dates to a factor at one of these levels "yearmonth", "yearquarter", "quarter", "month"

Usage

date_factor(dateVec, type = "yearmonth", minDate = min(dateVec, na.rm =
  TRUE), maxDate = max(dateVec, na.rm = TRUE))

Arguments

dateVec

A vector of date values

type

One of "year", "yearquarter", "yearmonth", "quarter", "month"

minDate

(Default = min(dateVec)) When determining factor levels, use this date to set the min level, after coercing dates to the specified type. For example, if dateVec = (2016-01-15, 2016-02-15), type = "yearmonth", and minDate = 2016-02-01, the result will be (NA, Feb 2016).

maxDate

(Default = max(dateVec)) When determining factor levels, use this date to set the max level. (See minDate, above)

Details

The resulting vector is an ordered factor of the specified type (e.g. yearmonth)

Examples

library(data.table)
dts <- as.Date(c("2014-1-1", "2015-1-15", "2015-6-1"))
date_factor(dts, type = "yearmonth")
date_factor(dts, type = "yearquarter")
date_factor(
  dateVec = dts, 
  type = "yearquarter", 
  minDate = as.Date("2015-1-1"), 
  maxDate = as.Date("2015-12-31")
)
date_factor(
  dateVec = as.Date(character(0)), 
  type = "yearmonth", 
  minDate = as.Date("2016-1-1"), 
  as.Date("2016-12-31")
)


[Package mltools version 0.3.5 Index]