无法使用read.zoo读取

时间:2012-05-29 04:16:31

标签: r zoo

我使用write.zoo创建了这个数据集,我使用了以下代码:

z <- structure(c(9.82, 9.83, 9.82, 9.9, 9.81, 9.83, 12.57, 12.57, 
12.57, 12.57, 12.57, 12.57, 2.75, 2.74, 2.75, 2.67, 2.76, 2.74
), .Dim = c(6L, 3L), .Dimnames = list(c("15", "14", "13", "12", 
"11", "10"), c("depth", "from_sensor_to_river_bottom", "Depth_from_river_surface_to_bottom"
)), index = structure(c(1337810422, 1337811320, 1337812220, 1337813118, 
1337814021, 1337814919), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo")

因此,z是一个动物园对象:

z
   depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom
15  9.82                       12.57                               2.75
14  9.83                       12.57                               2.74
13  9.82                       12.57                               2.75
12  9.90                       12.57                               2.67
11  9.81                       12.57                               2.76
10  9.83                       12.57                               2.74
attr(,"index")
[1] "2012-05-23 23:00:22 BST" "2012-05-23 23:15:20 BST" "2012-05-23 23:30:20 BST" "2012-05-23 23:45:18 BST"
[5] "2012-05-24 00:00:21 BST" "2012-05-24 00:15:19 BST"
attr(,"class")
[1] "zoo"

我使用以下方法将其写入文件:

 write.zoo(z, file = "y.txt", row.names=1:length(z[,1]),col.names=NULL)

这是输出文件“y.txt”的样子:

 "Index" "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
 "1" 2012-05-23 15:00:22 9.82 12.57 2.75
 "2" 2012-05-23 15:15:20 9.83 12.57 2.74
 "3" 2012-05-23 15:30:20 9.82 12.57 2.75
 "4" 2012-05-23 15:45:18 9.9 12.57 2.67
 "5" 2012-05-23 16:00:21 9.81 12.57 2.76

我正试着用它来阅读:

 read.zoo("y.txt", tz="")

这给了我这个错误:

  Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 1 did not have 6 elements.

我无法弄清问题是什么?还有问题是我写文件还是阅读文件?

感谢您的考虑。感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

问题是由时间戳引起的: 没有引用值,以及日期和时间之间的空格 被错误地理解为列分隔符。

您可以使用其他列分隔符来解决问题。

write.zoo(z, file = "y.txt", sep="\t",
  row.names=1:length(z[,1]),col.names=NULL
)
read.zoo("y.txt", tz="", sep="\t")

答案 1 :(得分:0)

我认为在标题中你缺少一个列名。 添加后,安装chron库并运行此

read.zoo(“zoo.txt”,FUN = as.chron)