-1

I'm trying to parse a vector tsl into a date class.

tsl <- c("Dec-2011", "Dec-2011", "Dec-2011", "Dec-2011", "Dec-2011", 
"Dec-2011")

I read the documentation of as.Date and I thought that as.Date(tsl, "%b-Y") should do the job, but it returns NAs.

I've also tried the following:

as.Date(tsl, "bbb-YYYY")
as.Date(tsl, "by")

Then I tried using lubridate but this didn't work either:

library(lubridate)
parse_date_time(tsl, "%b-%y")

Running Sys.getlocale() returns:

"LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"

Any ideas how to solve it?

epo3
  • 2,991
  • 2
  • 33
  • 60
  • 1
    `Date` objects are representations of calendar days as integers, and "Dec-2011" is ambiguous – rawr May 06 '16 at 16:00

1 Answers1

1

If you can live with year-month-day dates, here is a solution:

as.Date(gsub("^", "01-", tsl), format="%d-%b-%Y")

I added on the first of the month using gsub.

lmo
  • 37,904
  • 9
  • 56
  • 69