我使用以下方法加载了一个 csv 文件:
data = read.csv(file="/home/stefanos/R/data_frames_new/temp2.csv", header=TRUE, sep=",")
temp2.csv 文件的前 4 行是:
nodeId,battery,date,idHistoric,temperature,longitude,latitude
3,78.00,2013-04-01 00:04:03,30163676,13.74,-3.80176,43.46192
3,78.00,2013-04-01 00:09:01,30164278,13.67,-3.80176,43.46192
3,78.00,2013-04-01 00:13:59,30164875,13.67,-3.80176,43.46192
我想按 nodeId 对其进行分组,并找到每 15 分钟的温度平均值。所以我输入:
df <- xts(x = data[, c("nodeId", "battery", "idHistoric", "temperature", "longitude", "latitude")], order.by = as.POSIXct(data[, "date"], tz = "GMT", format = "%Y-%m-%d %H:%M:%S"))
接着:
df2 <- by(df,df$nodeId,function(x){
ends <- endpoints(x, on = "minutes", k = 15)
xx <- period.apply(x, ends, mean)
})
我的问题是我无法将 df2 写入 csv 文件。我还不能这样做。当我在屏幕上打印 df2 时,我看到以下结构:
/*********************************************/
INDICES: 3
nodeId battery idHistoric temperature longitude latitude
2013-04-01 00:13:59 3 78.00000 30164276 13.69333 -3.80176 43.46192
2013-04-01 00:28:54 3 79.00000 30166075 13.78000 -3.80176 43.46192
[...]
------------------------------------------------------------
INDICES: 4
nodeId battery idHistoric temperature longitude latitude
2013-04-01 00:13:07 4 87.00000 30164172 14.42667 -3.80098 43.46199
2013-04-01 00:28:01 4 87.33333 30165964 14.49000 -3.80098 43.46199
------------------------------------------------------------
INDICES: 5
nodeId battery idHistoric temperature longitude latitude
2013-04-01 00:13:31 5 83.00000 30164224 13.84667 -3.80058 43.46203
2013-04-01 00:28:26 5 83.66667 30166018 14.06000 -3.80058 43.46203
------------------------------------------------------------
INDICES: 6
nodeId battery idHistoric temperature longitude latitude
2013-04-01 00:12:52 6 78.00000 30164128 13.99667 -3.79979 43.46212
2013-04-01 00:28:52 6 79.00000 30165983 13.97333 -3.79979 43.46212
/*********************************************/
那么如何将其保存为 CSV?