2011-01-26 74 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")))