generate_factor_from_date {dataPreparation}R Documentation

Generate factor from dates

Description

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

Usage

generate_factor_from_date(
  data_set,
  cols = "auto",
  type = "yearmonth",
  drop = FALSE,
  verbose = TRUE,
  ...
)

Arguments

data_set

Matrix, data.frame or data.table

cols

List of date column(s) name(s) of data_set 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

data_set with new columns. data_set is edited by reference.

Examples

# Load set, and find dates
data(tiny_messy_adult)
tiny_messy_adult <- find_and_transform_dates(tiny_messy_adult, verbose = FALSE)

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


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

[Package dataPreparation version 1.1.1 Index]