2011-11-27 71 views

回答

1

如果DF然後我們的測試數據取均值在具有同一年所有數據點和月給動物園對象z並繪製它:

# test data 
DF <- data.frame(Time = as.POSIXct(Sys.Date() + 1:1000), data = 1:1000) 

library(zoo) 
z <- read.zoo(DF, aggregate = mean, FUN = as.yearmon) 
plot(z, type = "h") 
+0

感謝那些工作完美。爲了製作直方圖,我將'1'指定爲'數據'並設置aggregate = sum – Nasir

0

解析它們(例如,通過as.POSIXct()),以便適當的DateTime對象。

然後使用一個聚合例程,例如zoo和xts包具有時間索引結構,或ddply更通用,或者可以使用base R函數。

你沒有說什麼你想在直方圖中顯示。只是計數?在這種情況下,這是一個簡單的例子:

R> set.seed(42)  # fix RNG 
R> zz <- data.frame(val=runif(100), ts=Sys.time() - 6*31*24*60*60*runif(100)) 
R> summary(zz)   # values over June to Nov 2011 period 
     val     ts       
Min. :0.000239 Min. :2011-06-01 09:56:20.50 
1st Qu.:0.259673 1st Qu.:2011-07-10 01:43:58.81 
Median :0.539714 Median :2011-08-14 22:19:12.73 
Mean :0.524479 Mean :2011-08-22 17:57:00.34 
3rd Qu.:0.763614 3rd Qu.:2011-10-11 10:24:16.34 
Max. :0.988892 Max. :2011-11-27 03:51:25.63 
R> zz$mon <- as.POSIXlt(zz$ts)$mon + 1 
R> summary(zz)   # now we have the month as a column 
     val     ts        mon  
Min. :0.000239 Min. :2011-06-01 09:56:20.50 Min. : 6.00 
1st Qu.:0.259673 1st Qu.:2011-07-10 01:43:58.81 1st Qu.: 7.00 
Median :0.539714 Median :2011-08-14 22:19:12.73 Median : 8.00 
Mean :0.524479 Mean :2011-08-22 17:57:00.34 Mean : 8.29 
3rd Qu.:0.763614 3rd Qu.:2011-10-11 10:24:16.34 3rd Qu.:10.00 
Max. :0.988892 Max. :2011-11-27 03:51:25.63 Max. :11.00 
R> ddply(zz, .(mon), "nrow") # so count rows by month 
    mon nrow 
1 6 17 
2 7 22 
3 8 18 
4 9 15 
5 10 14 
6 11 14 
R> 

你可以做一個簡單的直方圖這些計數按月。