1

I am trying to convert string variables written as "Mon-YY" into the date class using as.Date, but I am getting a string of NAs. I used the following code:

cal_month = c("Feb-12", "Mar-12", "Apr-12", "May-12", "Jun-12", "Jul-12", "Aug-12", "Sep-12", "Oct-12")
test_Date = as.Date(cal_month, "%b-%y")

I got the following output for test_Date:

NA NA NA NA NA NA NA NA NA

I saw that there was some previous discussion about setting my C locale, so I did the following, but it made no difference. I got the exact same output as above.

lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")

cal_month = c("Feb-12", "Mar-12", "Apr-12", "May-12", "Jun-12", "Jul-12", "Aug-12", "Sep-12", "Oct-12")
test_Date = as.Date(cal_month, "%b-%y")

Sys.setlocale("LC_TIME", lct)
Andrie
  • 176,377
  • 47
  • 447
  • 496
user3798112
  • 17
  • 1
  • 4
  • 2
    Your months need to have a day in order to be a date. – Matthew Plourde Jul 02 '14 at 15:00
  • 1
    The **zoo** package has facilities for dealing with year-month data. But without a day, as mentioned, what you have isn't a date. – joran Jul 02 '14 at 15:03
  • And [**here**](http://stackoverflow.com/questions/11252989/change-factor-to-a-date-format-in-r/11253097#11253097) or [**here**](http://stackoverflow.com/questions/13386091/formatting-month-abbreviations-using-as-date/13386510#13386510) – Henrik Jul 02 '14 at 15:17
  • Sorry for the duplicate question. Your responses were very helpful. The zoo package works very well. Thank you!! – user3798112 Jul 02 '14 at 16:20

1 Answers1

-1

A date must have a day number.

Daniel
  • 645
  • 7
  • 11