Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort year-month column by year AND month

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

like image 439
Kasper Christensen Avatar asked Feb 27 '13 03:02

Kasper Christensen


1 Answers

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"
like image 152
Gavin Simpson Avatar answered Oct 06 '22 00:10

Gavin Simpson