Time series variable transformation

Hello everyone.
Another day, another question.
What Am I doing wrong here?

I want to transform the variable from "Apr 2016" to "01-04-2016"
I have no idea why isnt working.

dat <- data.frame(
  dates = c("Apr 2016","Apr 2017","Apr 2018","Apr 2019")
)

dat$dates <- paste0("1 ", dat$dates)
dat$dates <- as.Date(x = dat$dates , format = "%d%b%Y")

Thank you in advance

your dates have spaces so

format = "%d %b %Y"

also you could avoid needing to use the format tags if you go with lubridate::dmy()

dat <- data.frame(
  dates = c("Apr 2016","Apr 2017","Apr 2018","Apr 2019")
)

dat$dates <- paste0("1 ", dat$dates)
# dat$dates <- as.Date(x = dat$dates , format = "%d %b %Y")
# alternative :
dat$dates <- lubridate::dmy(dat$dates )



2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.