Learn R Programming

dataPreparation (version 0.4.3)

generateDateDiffs: Date difference

Description

Perform the differences between all dates of the dataSet set and optionally with a static date.

Usage

generateDateDiffs(
  dataSet,
  cols = "auto",
  analysisDate = NULL,
  units = "years",
  drop = FALSE,
  verbose = TRUE,
  ...
)

Arguments

dataSet

Matrix, data.frame or data.table

cols

List of date column(s) name(s) of dataSet to comute difference on. To transform all dates, set it to "auto". (character, default to "auto")

analysisDate

Static date (Date or POSIXct, optional)

units

Unit of difference between too dates (string, default to 'years')

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 (as a data.table) with more columns. A numeric column has been added for every couple of Dates. The result is in years.

Details

units is the same as difftime units, but with one more possiblity: years.

Examples

Run this code
# NOT RUN {
# First build a useful dataSet set
require(data.table)
dataSet <- data.table(ID = 1:100, 
                  date1 = seq(from = as.Date("2010-01-01"), 
                              to = as.Date("2015-01-01"), 
                              length.out = 100), 
                  date2 = seq(from = as.Date("1910-01-01"), 
                              to = as.Date("2000-01-01"), 
                              length.out = 100)
                  )

# Now let's compute
dataSet <- generateDateDiffs(dataSet, cols = "auto", analysisDate = as.Date("2016-11-14"))
# }

Run the code above in your browser using DataLab