date2factor: Date to factor time bin

View source: R/date2factor.R

date2factorR Documentation

Date to factor time bin

Description

Convert Date to time bin factor.

Usage

date2factor(
  x,
  time_bin = c("year", "quarter", "month", "day"),
  make_bins = c("range", "present"),
  bin_range = range(x, na.rm = TRUE),
  ordered = FALSE
)

Arguments

x

Date vector

time_bin

Character: "year", "quarter", "month", or "day"

make_bins

Character: "range" or "preseent". If "range" the factor levels will include all time periods define by time_bin within bin_range. This means factor levels can be empty. Otherwise, if "present", factor levels only include time periods present in data.

bin_range

Date, vector, length 2: Range of dates to make levels for. Defaults to range of input dates x

ordered

Logical: If TRUE, factor output is ordered. Default = FALSE

Details

Order of levels will be chronological (important e.g. for plotting) Additionally, can output ordered factor with ordered = TRUE

Value

factor of time periods

Author(s)

E.D. Gennatas

Examples

## Not run: 
library(data.table)
startDate <- as.Date("2018-01-01")
endDate <- as.Date("2020-12-31")
time <- sample(seq(startDate, endDate, length.out = 100))
date2factor(time)
date2factor(time, "quarter")
date2factor(time, "month")
date2factor(time, "day")
# range vs present
x <- sample(seq(as.Date("2018-01-01"), as.Date("2021-01-01"), by = 1), 10)
date2factor(x, time_bin = "quarter", make_bins = "present")
date2factor(x, time_bin = "quarter", make_bins = "range")

## End(Not run)

egenn/rtemis documentation built on Dec. 17, 2024, 6:16 p.m.