大家好,我正在尝试用日期来解决R中的一个小问题。我有一个名为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"
我想创建一个名为mydate2
的因子,其级别保持原始mydate
的顺序。我尝试了下一个,但我没有将订单保持为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
但是当我看到我的新因素mydate2
的水平时,我得到了这个:
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"
我不知道是否有可能获得一个因素,例如第一级ene 2009
,第二级feb 2009
等。和最后一级dic 2013
。我希望按照mydate2的级别顺序保持时间顺序。非常感谢你的帮助。
答案 0 :(得分:4)
试试这个:
mydate.ch <- format(mydate, "%b %Y")
mydate.fac <- factor(mydate.ch, unique(mydate.ch))
如果日期是唯一的,我们可以通过省略unique
,即factor(mydate.ch, mydate.ch)
来缩短日期。
答案 1 :(得分:2)
这也有效:设置ordered=TRUE
,根据需要格式化文本表示,但保留原始值(及其顺序):
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>