2011-01-26 72 views
5

我有2列中,第一包含日期,和含第二次的数据帧处理,在格式:使用日期和时间中的R

 date   time   
[1,] "2003-10-03" "22:32:00" 
[2,] "2003-10-05" "17:43:06" 
[3,] "2003-10-10" "18:45:56" 
[4,] "2003-11-12" "17:07:16" 
[5,] "2003-11-13" "12:48:04" 
[6,] "2003-11-13" "18:17:57" 

我想创建这些的一些直方图数据可以查看每年,每月和每天特定时间的事件数量。

对于容易现在

hist(as.Date(df[,1]), "years") 

,以获取事件的每月数(不考虑年)年,我用:

months = c("January", "February", "March", 
      "April", "May", "June", "July", 
      "August", "September", "October", 
      "November", "December") 
tb <- table(factor(months.Date(dt), levels=months) 
barplot(tb) 

问题:

  1. 是有没有更好的方法来做直方图每月?
  2. 我该如何在一天中的某个时间做同样的事情(小时垃圾桶是否足够)?

感谢

+4

无需列出月份名称 - 记住,'一个月。 name`和`month.abb`已经内置并使用你系统的默认语言。 – 2011-01-26 20:59:36

+0

@jonw:很高兴知道,我不知道! – nico 2011-01-27 00:28:29

回答

6

我会用XTS,特别是如果你有超过日期和时间在您的data.frame其他数据。

df$count <- 1 
x <- xts(df$count,as.POSIXct(paste(df$date,df$time))) 

# create aggregates and plot with plot.zoo() 
plot.zoo(period.apply(x, endpoints(index(x),"years"), sum), type="h") 
plot.zoo(period.apply(x, endpoints(index(x),"quarters"), sum), type="h") 
plot.zoo(period.apply(x, endpoints(index(x),"months"), sum), type="h") 
plot.zoo(period.apply(x, endpoints(index(x),"weeks"), sum), type="h") 
plot.zoo(period.apply(x, endpoints(index(x),"days"), sum), type="h") 
plot.zoo(period.apply(x, endpoints(index(x),"hours"), sum), type="h") 
+0

看起来很有趣,我会尽力的! – nico 2011-01-27 00:27:53

4

如果你不介意的标签是“01”,而不是“一”你可以做这样的事情

barplot(table(format(df$date,"%m")))