Learn R Programming

dataPreparation (version 0.4.3)

setColAsDate: Set columns as POSIXct

Description

Set as POSIXct a character column (or a list of columns) from a data.table.

Usage

setColAsDate(dataSet, cols = NULL, format = NULL, verbose = TRUE)

Arguments

dataSet

Matrix, data.frame or data.table

cols

List of column(s) name(s) of dataSet to transform into dates

format

Date's format (function will be faster if the format is provided) (character or list of character, default to NULL). For timestamps, format need to be provided ("s" or "ms" or second or millisecond timestamps)

verbose

Should the function log (logical, default to TRUE)

Value

dataSet (as a data.table), with specified columns set as Date. If the transformation generated only NA, the column is set back to its original value.

Details

setColAsDate is way faster when format is provided. If you want to identify dates and format automatically, have a look to identifyDates. If input column is a factor, it will be returned as a POSIXct column. If cols is kept to default (NULL) setColAsDate won't do anything.

Examples

Run this code
# NOT RUN {
# Lets build a dataSet set
dataSet <- data.frame(ID = 1:5, 
                  date1 = c("2015-01-01", "2016-01-01", "2015-09-01", "2015-03-01", "2015-01-31"), 
                  date2 = c("2015_01_01", "2016_01_01", "2015_09_01", "2015_03_01", "2015_01_31")
                  )

# Using setColAsDate for date2
data_transformed <- setColAsDate(dataSet, cols = "date2", format = "%Y_%m_%d")

# Control the results
lapply(data_transformed, class)

# With multiple formats:
data_transformed <- setColAsDate(dataSet, format = list(date1 = "%Y-%m-%d", date2 = "%Y_%m_%d"))
lapply(data_transformed, class)

# It also works with timestamps
dataSet <- data.frame(time_stamp = c(1483225200, 1485990000, 1488495600))
setColAsDate(dataSet, cols = "time_stamp", format = "s")
# }

Run the code above in your browser using DataLab