0

I have some data in year-month form that I want to format for graphing in ggplot.

date <- c("2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", 
"2016-09", "2016-10", "2016-11", "2016-12")

I was using parsedate::parse_date, but since updating R it no longer functioning.

I have looked at

Format Date (Year-Month) in R

as.yearmon works fine but it doesn't format to POSIXct which I need for ggplot. Other formatting such as as.POSIXct and strptime are giving NAs or errors.

Note: I don't mind if the first of the month gets added to the "year-mo" format.

phaser
  • 565
  • 1
  • 11
  • 28

1 Answers1

5

That's a FAQ: a date is comprised of day, month and year. You are missing one part. So by adding a day, say, '-01', you can impose the missing string structure and parse. Or you can use a more tolerant parser:

R> library(anytime)
R> anydate("2017-06")
[1] "2017-06-01"
R> 

Which works for your data too:

R> date
 [1] "2016-03" "2016-04" "2016-05" "2016-06"
 [5] "2016-07" "2016-08" "2016-09" "2016-10"
 [9] "2016-11" "2016-12"
R> anydate(date)
 [1] "2016-03-01" "2016-04-01" "2016-05-01"
 [4] "2016-06-01" "2016-07-01" "2016-08-01"
 [7] "2016-09-01" "2016-10-01" "2016-11-01"
[10] "2016-12-01"
R> 

Lastly, your request for POSIXct type is still short an hour, minute and second. But by the same principle:

R> anytime(date)
 [1] "2016-03-01 CST" "2016-04-01 CDT"
 [3] "2016-05-01 CDT" "2016-06-01 CDT"
 [5] "2016-07-01 CDT" "2016-08-01 CDT"
 [7] "2016-09-01 CDT" "2016-10-01 CDT"
 [9] "2016-11-01 CDT" "2016-12-01 CST"
R> 

These two functions return proper Date and POSIXct types, respectively.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • That's really helpful; does this imply, however, that every *POSIXct* item **has** to have all these attributes? Asking since I was looking at [rounding a POSIX date](https://stackoverflow.com/questions/11325631/round-a-posix-date-posixct-with-base-r-functionality) earlier & understood that one can drop the hour etc when rounding for days (`round (X, "days")`. Is that false, or would the result no longer be a valid *POSIX* date item? – patrick Jun 11 '17 at 21:45
  • 1
    How would you define a point in time lacking hours, minutes and seconds? Yes, converters exists but the _set missing values to zero_. – Dirk Eddelbuettel Jun 11 '17 at 21:47