So I have this long dataset, where in one column I have a date specified as character in format YYYMMM, but month abbreviated. So for example 1995MAR, 1995APR and so on. How can I transform that to date format?
I tried as.Date but it obviously hasn't worked, and with lubridate::ymd which hasn't worked as well.
CodePudding user response:
Using parse_date_time
from lubridate
date <- "1995MAR"
library(lubridate)
parse_date_time(date, order = "Yb")
Output:
[1] "1995-03-01 UTC"
Alternatively using zoo
library(zoo)
as.Date(as.yearmon(date, '%Y%b'))
Output:
"1995-03-01"
str(as.Date(as.yearmon(date, '%Y%b')))
Date[1:1], format: "1995-03-01"
CodePudding user response:
In Base R, add a day number to parse:
date <- "1995MAR"
as.Date(paste(date, "01"), format = "%Y%b %d")
#[1] "1995-03-01"