0

I have a date that is in the this format:

chr [1:56] "Sep-2016" "Oct-2016" "Nov-2016" "Dec-2016" "Jan-2017" "Feb-2017" "Mar-2017" "Apr-2017"

I tried as.Date(Dates, "%b-%Y") and got NA for all the values. For some reason I have tried multiple ways and using different format but it is still not working. I am looking to get it into either 09-2016, 10-2016 or just simply turn it into a date format.

Any help is much appreciated!

haroldchoi
  • 43
  • 9

1 Answers1

1

Date class needs a day as well. Easiest is to convert to yearmon class from zoo and then coerce it to Date, which adds a dummy day

library(zoo)
as.Date(as.yearmon(Dates, '%b-%Y'))

or in base R, paste a day and convert

as.Date(paste0(Dates, '-01'), '%b-%Y-%d')
akrun
  • 874,273
  • 37
  • 540
  • 662