-1

Hello, I cannot figure out where the problem is even after consulting the documentation for as.Date, although it must be a very silly thing. I have a character vector DD that contains dates:

 unique(DD)
      [1] NA        "1999-01" "2013-08" "2013-02" "2013-03" "2013-01" "2013-07" "2012-12" "2013-05"
     [10] "2012-11" "2013-04" "2013-06" "2011-11" "2011-12" "2011-06" "2010-07" "2011-01" "2010-03"

I want to convert it into date format. I tried

DD2 = as.Date(DD, format = "%Y-%m")

but the result gives only NAs :

 unique(DD2)
[1] NA

Can anyone see the problem?

Giuseppe
  • 518
  • 10
  • 22
  • 2
    I'm pretty sure the `Date` class requires year, month, and day elements. You could just make each element the first of the month like this: `as.Date(paste0(na.omit(DD),"-01"))` – nrussell Oct 01 '14 at 16:56
  • 1
    http://stackoverflow.com/questions/6242955/converting-year-and-month-to-a-date-in-r – GSee Oct 01 '14 at 16:57
  • @GSee Drat, that's a better duplicate. – joran Oct 01 '14 at 16:58

1 Answers1

0

I'm not really sure why this doesn't work, but it appears you do have to completely specify the date for as.Date to work. Instead, you could use as.yearmon from the zoo package.

peterhurford
  • 1,074
  • 1
  • 13
  • 21