mmyy: Define month-year categories from time series

View source: R/time_categories.R

mmyyR Documentation

Define month-year categories from time series

Description

This function defines month-year categories from time series (i.e., Date and DateTimeClasses objects).

Usage

mmyy(x, levels = TRUE)

Arguments

x

An vector of class Date or DateTimeClasses.

levels

A logical input which defines whether or not to return a factor with chronologically ordered levels (i.e., levels ordered by year then month, for the inputted x) or a character (levels = FALSE).

Value

The function returns a vector which specifies the month and year of each observation in x as mm-yyyy. By default (i.e., when levels = TRUE), this is a factor with chronologically ordered levels (i.e., levels ordered by year then month, for the inputted x). If levels = FALSE, a character vector is returned.

Author(s)

Edward Lavender

Examples

#### Example (1): Extract the month-year category from Dates:
mmyy(as.Date("2016-01-01"))
mmyy(seq.Date(as.Date("2016-01-01"), as.Date("2016-12-01"), 10))

#### Example (2): Extract the month-year category from POSIXct objects:
mmyy(as.POSIXct("2016-01-01"))
mmyy(seq.POSIXt(as.POSIXct("2016-01-01"), as.POSIXct("2016-12-01"), "20 days"))

#### Example (3): By default, mmyy() returns an ordered factor
# ... with levels ordered chronologically:
mmyy(c(as.POSIXct(c("2017-01-02", "2016-01-02", "2016-03-01", "2017-02-03"))))

#### Example (4): Ordered levels can be suppressed with levels = FALSE,
# ... in which case a character vector is returned:
mmyy(as.Date("2016-01-01"), levels = FALSE)
mmyy(seq.Date(as.Date("2016-01-01"), as.Date("2016-12-01"), 10), levels = FALSE)
mmyy(as.POSIXct("2016-01-01"), levels = FALSE)
mmyy(seq.POSIXt(as.POSIXct("2016-01-01"), as.POSIXct("2016-12-01"), "20 days"), levels = FALSE)


edwardlavender/Tools4ETS documentation built on Nov. 29, 2022, 7:41 a.m.