date2factor | R Documentation |
Convert Date to time bin factor.
date2factor(
x,
time_bin = c("year", "quarter", "month", "day"),
make_bins = c("range", "present"),
bin_range = range(x, na.rm = TRUE),
ordered = FALSE
)
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 |
bin_range |
Date, vector, length 2: Range of dates to make levels for. Defaults to range of
input dates |
ordered |
Logical: If TRUE, factor output is ordered. Default = FALSE |
Order of levels will be chronological (important e.g. for plotting)
Additionally, can output ordered factor with ordered = TRUE
factor of time periods
E.D. Gennatas
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.