将xts对象保存为csv文件

时间:2013-04-13 13:07:22

标签: r csv statistics xts

我使用:

加载了一个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?

3 个答案:

答案 0 :(得分: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

编辑以保存/再次将其作为动物园对象进行阅读,我略微改变了绑定列表的rownames:

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

<强> EDIT2

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

do.call(rbind, unname(df2))

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

答案 1 :(得分: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编写xtszoo个对象的csv文件很方便:

write.zoo(as.xts(do.call(rbind, unname(df2))), file="test.csv", sep=",")

答案 2 :(得分: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
}