3

我使用以下方法加载了一个 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?

4

3 回答 3

3

你可以这样做(正如@Roland在评论中提到的那样)

write.table(do.call(rbind,df2),file='test.csv')

这是您的数据的完整示例。您可以使用read.zoo一个线性命令来创建您的 xts 对象:

library(zoo)
## you replace text= here by file=temp2.csv
dat <- read.zoo(text='nodeId,battery,date,idHistoric,temperature,longitude,latitude
3,78.00,2013-04-01 00:13:59,30163676,13.74,-3.80176,43.46192
3,78.00,2013-04-01 00:28:54,30163676,13.74,-3.80176,43.46192
4,78.00,2013-04-01 00:13:07,30164278,13.67,-3.80176,43.46192
4,78.00,2013-04-01 00:28:01,30163676,13.74,-3.80176,43.46192
5,78.00,2013-04-01 00:13:31,30163676,13.74,-3.80176,43.46192
5,78.00,2013-04-01 00:28:26,30164875,13.67,-3.80176,43.46192
6,78.00,2013-04-01 00:12:52,30164875,13.67,-3.80176,43.46192
6,78.00,2013-04-01 00:28:52,30164875,13.67,-3.80176,43.46192',header=TRUE,
                tz='',sep=',',index=3)

然后您按组创建并保存您的列表,

library(xts)
df2 <- by(dat,dat$nodeId,function(x){
  ends <- endpoints(x, on = "minutes", k = 1)
  xx    <- period.apply(x, ends, mean)
})

write.table(do.call(rbind,df2),file='test.csv')

要再次阅读它,您只需

read.table('test.csv')
                      nodeId battery idHistoric temperature longitude latitude
3.2013-04-01 00:13:59      3      78   30163676       13.74  -3.80176 43.46192
3.2013-04-01 00:28:54      3      78   30163676       13.74  -3.80176 43.46192
4.2013-04-01 00:13:07      4      78   30164278       13.67  -3.80176 43.46192
4.2013-04-01 00:28:01      4      78   30163676       13.74  -3.80176 43.46192
5.2013-04-01 00:13:31      5      78   30163676       13.74  -3.80176 43.46192
5.2013-04-01 00:28:26      5      78   30164875       13.67  -3.80176 43.46192
6.2013-04-01 00:12:52      6      78   30164875       13.67  -3.80176 43.46192
6.2013-04-01 00:28:52      6      78   30164875       13.67  -3.80176 43.46192

编辑保存/并再次将其作为动物园对象读取,我稍微转换了绑定列表的行名:

dd <- do.call(rbind,df2)
rownames(dd) <- gsub('*.[.]','',rownames(dd))
write.table(dd,file='test.csv')

现在我可以再次阅读:

read.zoo('test.csv',index=0,tz='')
                    nodeId battery idHistoric temperature longitude latitude
2013-04-01 00:12:52      6      78   30164875       13.67  -3.80176 43.46192
2013-04-01 00:13:07      4      78   30164278       13.67  -3.80176 43.46192
2013-04-01 00:13:31      5      78   30163676       13.74  -3.80176 43.46192
2013-04-01 00:13:59      3      78   30163676       13.74  -3.80176 43.46192
2013-04-01 00:28:01      4      78   30163676       13.74  -3.80176 43.46192
2013-04-01 00:28:26      5      78   30164875       13.67  -3.80176 43.46192
2013-04-01 00:28:52      6      78   30164875       13.67  -3.80176 43.46192
2013-04-01 00:28:54      3      78   30163676       13.74  -3.80176 43.46192

编辑2

感谢@Gsee 出色的回答,您可以执行以下操作:

do.call(rbind, unname(df2))

这将保持行名正确,因此无需像我在之前的编辑中那样使用正则表达式。

于 2013-04-13T15:12:31.163 回答
2

如果你看一下str(df2),你会发现它是一个命名的list. 通常,当您有一个列表并且想要将其转换为单个对象时,您可以使用类似do.call(rbind, df2). 这与 相同rbind(df2[[1]], df2[[2]], df2[[3]], df2[[4]]),但适用于任意长度的列表。

在这种情况下,您listnames

> names(df2)
[1] "3" "4" "5" "6"

所以,如果你只是do.call(rbind, df2),那rownames将不是你想要的——它们会names在列表的前面加上。

> rownames(do.call(rbind, df2))
[1] "3.2013-04-01 00:13:59" "3.2013-04-01 00:28:54" "4.2013-04-01 00:13:07"
[4] "4.2013-04-01 00:28:01" "5.2013-04-01 00:13:31" "5.2013-04-01 00:28:26"
[7] "6.2013-04-01 00:12:52" "6.2013-04-01 00:28:52"

解决方案是unname列表

do.call(rbind, unname(df2))

由于您正在使用xts,您可能希望将其强制为xts对象:

> as.xts(do.call(rbind, unname(df2)))
                    nodeId battery idHistoric temperature longitude latitude
2013-04-01 00:12:52      6      78   30164875       13.67  -3.80176 43.46192
2013-04-01 00:13:07      4      78   30164278       13.67  -3.80176 43.46192
2013-04-01 00:13:31      5      78   30163676       13.74  -3.80176 43.46192
2013-04-01 00:13:59      3      78   30163676       13.74  -3.80176 43.46192
2013-04-01 00:28:01      4      78   30163676       13.74  -3.80176 43.46192
2013-04-01 00:28:26      5      78   30164875       13.67  -3.80176 43.46192
2013-04-01 00:28:52      6      78   30164875       13.67  -3.80176 43.46192
2013-04-01 00:28:54      3      78   30163676       13.74  -3.80176 43.46192

最后,我发现用它来编写或对象write.zoo的 csv 文件很方便:xtszoo

write.zoo(as.xts(do.call(rbind, unname(df2))), file="test.csv", sep=",")
于 2013-04-13T16:03:06.417 回答
1

解决了

xts 返回一个向量。我必须遍历每个位置(使用 while)并将每个元素写入 csv 文件。

df2 <- by(df,df$nodeId,function(x){
   ends <- endpoints(x, on = "minutes", k = 15)
   xx    <- period.apply(x, ends, mean)
})

i <- 1

total <- length(df2)

while( i <= total ){
    write.csv(df2[i],paste("lights_2013-04-0102/out_",i,".csv",sep = ""))
    i <- i + 1
}
于 2013-04-13T16:02:27.167 回答