set_col_as_date {dataPreparation} | R Documentation |
Set columns as POSIXct
Description
Set as POSIXct a character column (or a list of columns) from a data.table.
Usage
set_col_as_date(data_set, cols = NULL, format = NULL, verbose = TRUE)
Arguments
data_set |
Matrix, data.frame or data.table |
cols |
List of column(s) name(s) of data_set 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). |
verbose |
Should the function log (logical, default to TRUE) |
Details
set_col_as_date is way faster when format is provided. If you want to identify dates and format
automatically, have a look to identify_dates
.
If input column is a factor, it will be returned as a POSIXct column.
If cols
is kept to default (NULL) set_col_as_date won't do anything.
Value
data_set
(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.
Examples
# Lets build a data_set set
data_set <- data.frame(ID = seq_len(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 set_col_as_date for date2
data_transformed <- set_col_as_date(data_set, cols = "date2", format = "%Y_%m_%d")
# Control the results
lapply(data_transformed, class)
# With multiple formats:
data_transformed <- set_col_as_date(data_set, format = list(date1 = "%Y-%m-%d", date2 = "%Y_%m_%d"))
lapply(data_transformed, class)
# It also works with timestamps
data_set <- data.frame(time_stamp = c(1483225200, 1485990000, 1488495600))
set_col_as_date(data_set, cols = "time_stamp", format = "s")