In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you
In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you
An option would be parse_date_time
from lubridate
which can take multiple Date
formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate
from anytime
. But, applying anydate
, check whether all the format
s are already present with
library(anytime)
getFormats()
If some format
s are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate
on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
v1 <- c("14-nov-17", "14APR2016")
Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\\d{2})(\\w{3})(\\d{2})(\\d{2})', '\\1-\\2-\\4', data$date),
format = "%d-%b-%y")