提前谢谢你的帮助。我试着做一些我确信很简单的事情,但我想不出来。我想在R中做一个散射图,在x轴上的日期和y轴上的相对频率。问题是,由于R没有意识到数字是日期,所以x轴上的某些点都聚集在一起。日期是格式化的年月集,其中设定的日期是该月份内10天的第0或第1或第2组。所以12071是2012年的7月1日。我需要R来认识到,一个月有3组,一年有12个月,4年有,这样的散点图就有空间。解决这个问题的最好方法是什么?
下面是我的数据片段:
ID,Date,Trigram,Freq,Relfreq
TPN,12071,a constitutional convention,6,0.00001211467753757064371339095882
TPN,12111,a constitutional convention,2,0.000003302558987831721409334022467
TPN,11071,a constitutional convention,6,0.00001211467753757064371339095882
TPN,11111,a constitutional convention,2,0.000003302558987831721409334022467
TPN,10071,a constitutional convention,6,0.00001211467753757064371339095882
TPN,10111,a constitutional convention,2,0.000003302558987831721409334022467
TPN,09071,a constitutional convention,6,0.00001211467753757064371339095882
TPN,09111,a constitutional convention,2,0.000003302558987831721409334022467
CR,10032,a constitutional convention,3,0.000001049388049359016289650690200
CR,10062,a constitutional convention,2,7.020490002120187980640296770E-7我试着使用as.date(),就像在这个站点http://www.statmethods.net/input/dates.html上描述的那样,但我并不真正理解它。
> strdates <- origina_NoCon$Date
> dates <- as.Date(strdates, %y%m)
Error: unexpected SPECIAL in "dates <- as.Date(strdates, %y%"编辑:
这是dput(Strdates)输出的一部分:
> dput(strdates)
c(12071L, 12111L, 11071L, 11111L, 10071L, 10111L, 9071L, 9111L,
10032L, 10062L, 11041L, 11071L, 11111L, 11121L, 12020L, 12021L,
12102L, 12110L, 12111L, 11021L,...)发布于 2014-12-12 15:30:21
让我们首先将您的strdates更改为character,必要时用零填充:
strdates.chr <- sprintf("%05i",strdates)现在您可以首先重新格式化它们以表示每个月的第一个,然后将其转换为Date。
> as.Date(paste0(substr(strdates.chr,1,4),"01"),format="%y%m%d")
[1] "2012-07-01" "2012-11-01" "2011-07-01" "2011-11-01" "2010-07-01"
[6] "2010-11-01" "2009-07-01" "2009-11-01" "2010-03-01" "2010-06-01"
[11] "2011-04-01" "2011-07-01" "2011-11-01" "2011-12-01" "2012-02-01"
[16] "2012-02-01" "2012-10-01" "2012-11-01" "2012-11-01" "2011-02-01"为了包含最后一段信息,提取该信息(substr),转换为numeric,最后添加10天的适当倍数(向Dates添加整数将自动解释为添加了若干天):
> as.Date(paste0(substr(strdates.chr,1,4),"01"),format="%y%m%d")+
+ as.numeric(substr(strdates.chr,5,5))*10
[1] "2012-07-11" "2012-11-11" "2011-07-11" "2011-11-11" "2010-07-11"
[6] "2010-11-11" "2009-07-11" "2009-11-11" "2010-03-21" "2010-06-21"
[11] "2011-04-11" "2011-07-11" "2011-11-11" "2011-12-11" "2012-02-01"
[16] "2012-02-11" "2012-10-21" "2012-11-01" "2012-11-11" "2011-02-11"https://stackoverflow.com/questions/27446682
复制相似问题