2

我使用 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.

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

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

4

2 回答 2

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")
于 2012-05-29T05:23:51.037 回答
0

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

read.zoo("zoo.txt", FUN = as.chron)

于 2012-05-29T04:45:10.810 回答