Duck
Duck

Reputation: 39595

Transform a date to factor in R keeping order in factor levels considering original date

Hi everybody I am trying to solve a little problem in R with dates. I have the next date object named mydate:

mydate=seq(as.Date("2009-01-01"),as.Date("2013-12-01"), by="1 month")

mydate
 [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" "2009-06-01"
 [7] "2009-07-01" "2009-08-01" "2009-09-01" "2009-10-01" "2009-11-01" "2009-12-01"
[13] "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" "2010-05-01" "2010-06-01"
[19] "2010-07-01" "2010-08-01" "2010-09-01" "2010-10-01" "2010-11-01" "2010-12-01"
[25] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01" "2011-06-01"
[31] "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01" "2011-11-01" "2011-12-01"
[37] "2012-01-01" "2012-02-01" "2012-03-01" "2012-04-01" "2012-05-01" "2012-06-01"
[43] "2012-07-01" "2012-08-01" "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01"
[49] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01"
[55] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01"

I want to create a factor named mydate2 whose levels keep the order as original mydate. I try the next but I don't get keeping the order as mydate:

mydate2=factor(format(mydate,"%b %Y"))
mydate2
 [1] ene 2009 feb 2009 mar 2009 abr 2009 may 2009 jun 2009 jul 2009 ago 2009 sep 2009
[10] oct 2009 nov 2009 dic 2009 ene 2010 feb 2010 mar 2010 abr 2010 may 2010 jun 2010
[19] jul 2010 ago 2010 sep 2010 oct 2010 nov 2010 dic 2010 ene 2011 feb 2011 mar 2011
[28] abr 2011 may 2011 jun 2011 jul 2011 ago 2011 sep 2011 oct 2011 nov 2011 dic 2011
[37] ene 2012 feb 2012 mar 2012 abr 2012 may 2012 jun 2012 jul 2012 ago 2012 sep 2012
[46] oct 2012 nov 2012 dic 2012 ene 2013 feb 2013 mar 2013 abr 2013 may 2013 jun 2013
[55] jul 2013 ago 2013 sep 2013 oct 2013 nov 2013 dic 2013

But when I look levels of my new factor mydate2 I got this:

levels(mydate2)
 [1] "abr 2009" "abr 2010" "abr 2011" "abr 2012" "abr 2013" "ago 2009" "ago 2010"
 [8] "ago 2011" "ago 2012" "ago 2013" "dic 2009" "dic 2010" "dic 2011" "dic 2012"
[15] "dic 2013" "ene 2009" "ene 2010" "ene 2011" "ene 2012" "ene 2013" "feb 2009"
[22] "feb 2010" "feb 2011" "feb 2012" "feb 2013" "jul 2009" "jul 2010" "jul 2011"
[29] "jul 2012" "jul 2013" "jun 2009" "jun 2010" "jun 2011" "jun 2012" "jun 2013"
[36] "mar 2009" "mar 2010" "mar 2011" "mar 2012" "mar 2013" "may 2009" "may 2010"
[43] "may 2011" "may 2012" "may 2013" "nov 2009" "nov 2010" "nov 2011" "nov 2012"
[50] "nov 2013" "oct 2009" "oct 2010" "oct 2011" "oct 2012" "oct 2013" "sep 2009"
[57] "sep 2010" "sep 2011" "sep 2012" "sep 2013"

I don't know if it is possible to get a factor with for example first level ene 2009, second level feb 2009,etc. and the last level dic 2013. I wish to keep time order in the order of levels of mydate2. Many thanks for your help.

Upvotes: 1

Views: 5262

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

This also works: set ordered=TRUE, format the textual representation as you desired but keep the original values (and their ordering):

R> mydate3 <- factor(mydate, labels=format(mydate,"%b %Y"), ordered=TRUE)
R> mydate3
 [1] Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Jun 2009 Jul 2009 Aug 2009 
 [9] Sep 2009 Oct 2009 Nov 2009 Dec 2009 Jan 2010 Feb 2010 Mar 2010 Apr 2010 
[18] May 2010 Jun 2010 Jul 2010 Aug 2010 Sep 2010 Oct 2010 Nov 2010 Dec 2010
24 Levels: Jan 2009 < Feb 2009 < Mar 2009 < Apr 2009 < May 2009 < Jun 2009 
   < Jul 2009 < Aug 2009 < Sep 2009 < Oct 2009 < Nov 2009 < ... < Dec 2010
R> 

Upvotes: 3

G. Grothendieck
G. Grothendieck

Reputation: 269481

Try this:

mydate.ch <- format(mydate, "%b %Y")
mydate.fac <- factor(mydate.ch, unique(mydate.ch))

If the dates are unique as they are here we could shorten it by omitting unique, i.e. factor(mydate.ch, mydate.ch) .

Upvotes: 5

Related Questions