I’m working with monthly data and have a character vector of dates, formatted:
Sep/2012
Aug/2012
Jul/2012
and so on, back to 1981. I’ve tried using
as.Date(dates, "%b/%Y")
where %b represents month abbreviations, but this only returns NAs. What am I doing wrong?
Note: I already found a workaround using gsub() to add “01/” in front of each entry, like so:
01/Sep/2012
01/Aug/2012
01/Jul/2012
Then as.Dates() works, but this seems a little inelegant, and isn’t strictly accurate anyway.
You are looking for
as.yearmon()in the zoo package. Given your dateswe load the package and convert to the
"yearmon"classWhich gives
You can coerce to an object of class
"Date"using theas.Date()methodWhich would be a simpler way of getting the thing you did via
gsub(). There is afracargument which controls how far through the month the day component should be:But that may ont be sufficient for you.
If you really only want the dates stored as you have them, then they aren’t really dates but if you are happy to work within the zoo package then the
"yearmon"class can be used as an index for azooobject which is a time series.