5

I am trying to order a series of time data I have stored in a data frame. The format is of:

"%Y-%b"

Which looks like "2009-Sep"etc.

Until now I have managed to find this method:

ds[order(as.Date(ds$yearmonth, format = "%Y-%b")),]

But it only sort by year, and then it moves to alphabetical order regarding the months, giving me an order of 2009-Jan, 2009-Jul, 2009-Jun etc. I am quite puzzled this is not an easy problem to fix.

Please help...

Best Kasper

Henrik
  • 65,555
  • 14
  • 143
  • 159
Kasper Christensen
  • 895
  • 3
  • 10
  • 30
  • 1
    Have you looked at the output of `as.Date(ds$yearmonth, format="%Y-%b")`? – mnel Feb 27 '13 at 03:22
  • 1
    @mnel have you? That's not a valid Date. :-) – Gavin Simpson Feb 27 '13 at 03:23
  • 1
    @GavinSimpson, I have. I was helping the OP clarify his question. :-). – mnel Feb 27 '13 at 03:24
  • 1
    My point is, if that is all the data he has, short of pasting on a day to each year-month, `as.Date` is the wrong tool here. – Gavin Simpson Feb 27 '13 at 03:25
  • 1
    Yes, the question is a duplicate of http://stackoverflow.com/questions/6242955/converting-year-and-month-to-a-date-in-r. My comment perhaps should have been slightly more clear, and less tongue-in-cheek (or oblique). – mnel Feb 27 '13 at 03:29
  • Thansk for the help guys. And sorry for not doing my homework completely, but the question sometimes asking the right question can be hard. I never thought of asking the question as the one in http://stackoverflow.com/questions/6242955/converting-year-and-month-to-a-date-in-r. – Kasper Christensen Feb 27 '13 at 10:39

1 Answers1

13

The as.yearmon() function (and the "yearmon" class) in package zoo is designed for this sort of data:

dat <- c("2009-Sep","2009-Feb","2009-Jan")
require(zoo)
d2 <- as.yearmon(dat, "%Y-%b")
> sort(d2)
[1] "Jan 2009" "Feb 2009" "Sep 2009"
> order(d2)
[1] 3 2 1
> d2[order(d2)]
[1] "Jan 2009" "Feb 2009" "Sep 2009"

You could of course paste0() a day onto each date and coerce to class "Date" via as.Date() but as.yearmon() seems more natural to me:

> as.Date(paste0(dat, "-01"), "%Y-%b-%d")
[1] "2009-09-01" "2009-02-01" "2009-01-01"

Note you can generate that same result by coercing the "yearmon" object to class "as.Date", e.g.:

> as.Date(d2)
[1] "2009-09-01" "2009-02-01" "2009-01-01"
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • One should remember that the vector class should be "character". Mine was by default "factor" giving my "NA" values. – Kasper Christensen Feb 27 '13 at 10:46
  • 2
    @KasperChristensen Good point. That just goes to show the merits of reproducible examples. It is impossible to tell this from your question so I took you at your word that the data were character (that is what you show). `as.yearmon(as.character(dat), "%Y-%b"))` would be one way around the factor issue. Another would be to read your data in in the correct format in the beginning by specifying the variable classes, e.g. via `colClasses` in `read.table()` and its siblings.. – Gavin Simpson Feb 27 '13 at 14:46