[R] Extracting months and years from Dates while keeping order

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Fri May 6 22:07:19 CEST 2011


Hello!

I'd like to take Dates and extract from them months and years - but so
that it sorts correctly. For example:

x1<-seq(as.Date("2009-01-01"), length = 14, by = "month")
(x1)
order(x1)  # produces correct order based on full dates

# Of course, I could do "format" - but this way I am losing the "Date"
quality of the data:

x2<-format(x1,"%Y.%m")
(x2)
order(x2)

Not sure it's possible at all: But is there some other way to extract
just months and years from Dates (while ingoring days) - but so that
it's still somehow a Date object?

Thank you very much!

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list