2016-12-26 69 views
-1

我有一個大型數據集,我試圖重新格式化。不幸的是,我無法彙總每日數據。R計算來自不規則小時數據的每日平均數據

dataset1_updated<- structure(list(X = 1:5, Time = structure(c(1L, 1L, 2L, 3L, 3L), .Label = c("7/29/11 10:29", "7/29/11 10:30", "7/29/11 10:32" 
), class = "factor"), O3 = c(32.032608222367, 32.032608222367, 
32.032608222367, 32.032608222367, 32.032608222367), SO2 = c(2.611, 
2.605, 2.744, 2.767, 2.778), NO = c(0.081, 0.081, 0.081, 0.081, 
0.081), NO2 = c(1.938, 1.912, 1.912, 1.896, 1.863), NOx = c(2.019, 
1.993, 1.993, 1.977, 1.944)), .Names = c("X", "Time", "O3", 
"SO2", "NO", "NO2", "NOx"), row.names = c(NA, 5L), class = "data.frame") 

我將數據集轉換爲xts對象,並應用日均值函數,結果爲「NA」。你能告訴我缺少什麼嗎?

x <- as.xts(as.POSIXct(dataset1_updated$Time, format="%m/%d/%Y %H:%M")) 
x_up<- apply.daily(x, colMeans) 
write.csv(as.data.frame(as.matrix(x_up)), file="test") 

謝謝你,

+2

請提供[重複的例子] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)...一個圖像是不夠的... – digEmAll

+0

請提供一個可重複的例子。數據圖是可接受的。 –

+0

請給出輸出的整個輸出 - 你在這裏給出的是不完整的,不會運行 –

回答

0

我們需要爲as.xts僅在DateTime類,而不是整個數據集應用更改xts聲明

xt1 <- xts(dataset1_updated[-(1:2)], order.by = as.POSIXct(dataset1_updated$Time, 
           format = "%m/%d/%y %H:%M")) 
x_up <- apply.daily(xt1, colMeans) 
x_up 
#       O3 SO2 NO NO2 NOx 
#2011-07-29 10:32:00 32.03261 2.701 0.081 1.9042 1.9852 
+0

非常感謝@akrun – nil

+0

@nil謝謝你的迴應。你也可以勾選[here](http://stackoverflow.com/help/someone-answers) – akrun

相關問題