adam.888 adam.888 - 21 days ago 5
R Question

R converting a character to a date

I have a character string of type DEC 2016 and I want to convert it into a date.

I tried:

dates1 <- c("DEC 2016", "FEB 2017", "NOV 2018")

dates2 <- as.Date(dates1,format="%b %Y")


but this did not work.

Thank you for your help.

Answer
dates1 <- c("DEC 2016", "FEB 2017", "NOV 2018")

library(zoo)
dates2 <- as.Date(as.yearmon(dates1))

dates2
[1] "2016-12-01" "2017-02-01" "2018-11-01"
class(dates2)
[1] "Date"

You should really do some Googling.

Comments