Learn R Programming

dataPreparation (version 0.4.3)

generateFactorFromDate: Generate factor from dates

Description

Taking Date or POSIXct colums, and building factor columns from them.

Usage

generateFactorFromDate(
  dataSet,
  cols = "auto",
  type = "yearmonth",
  drop = FALSE,
  verbose = TRUE,
  ...
)

Arguments

dataSet

Matrix, data.frame or data.table

cols

List of date column(s) name(s) of dataSet to transform into factor. To transform all dates, set it to "auto". (characters, default to "auto")

type

"year", "yearquarter", "yearmonth", "quarter" or "month", way to aggregate a date, (character, default to "yearmonth")

drop

Should cols be dropped after generation (logical, default to FALSE)

verbose

Should the function log (logical, default to TRUE)

...

Other arguments such as name_separator to separate words in new columns names (character, default to ".")

Value

dataSet with new columns. dataSet is edited by reference.

Examples

Run this code
# NOT RUN {
# Load set, and find dates
data(messy_adult)
messy_adult <- findAndTransformDates(messy_adult, verbose = FALSE)

# Generate new columns
# Generate year month columns
messy_adult <- generateFactorFromDate(messy_adult, cols = c("date1", "date2", "num1"))
head(messy_adult[, .(date1.yearmonth, date2.yearmonth)])


# Generate quarter columns
messy_adult <- generateFactorFromDate(messy_adult, cols = c("date1", "date2"), type = "quarter")
head(messy_adult[, .(date1.quarter, date2.quarter)])
# }

Run the code above in your browser using DataLab