Learn R Programming

mltools (version 0.2.0)

date_factor: 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", fullyears = TRUE)

Arguments

dateVec

A vector of date values

type

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

fullyears

Should levels will always start and end at the end of a year? For example date_factor(as.Date(c("2016-4-1", "2016-7-1")), "yearquarter", fullyears=TRUE) will return a factor with four levels (Q1, Q2, Q3, Q4) even though only two date values were given.

Details

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

Examples

Run this code
library(data.table)
dts <- as.Date(c("2014-1-1", "2015-1-1", "2015-6-1"))
date_factor(dts, type="yearmonth")
date_factor(dts, type="yearquarter")
date_factor(dts, type="yearquarter", fullyears=FALSE)

Run the code above in your browser using DataLab