如何在xts中读取时间序列?

3

我有这些时间序列数据:

     "timestamp"          "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999

我将使用以下代码:

我正在使用以下代码:

d <- read.table(Name[1], header=TRUE)  #Name[1] is text file containing data

d <- read.zoo(d,
 format="'%Y-%m-%d %H:%M:%S'", 
 FUN=as.POSIXct  )

它给我返回了这个错误:

Error in read.zoo(d, format = "'%Y-%m-%d %H:%M:%S'", FUN = as.POSIXct) : 
 index has 5 bad entries at data rows: 1 2 3 4 5

我希望能得到关于这个问题的帮助。 谢谢您的考虑。


你的时间结尾处的“-05”位是什么意思? - Ben Bolker
@BenBolker 没错。这就是问题的原因,因为 as.POSIXct 不知道如何处理这个。 - Andrie
@BenBolker 看起来这是一个格式错误的时间戳。请看我的回答。 - Andrie
我不知道这是什么。当我从互联网获取数据时,它就出现了。我们能处理它吗? - rockswap
2个回答

4
您的时间戳数据包含格式错误的时区数据,即每个时间戳末尾的“-05”。

?strptime中得知,您可以使用%z来格式化带符号的时区偏移量,应该是一个带符号的四位数,例如-0500

%z
Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC.

因此,这里有一个解决方法,可以为您的时间戳添加缺失的 00

重新创建您的数据:

dat <- '
"timestamp" "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999
'

添加缺失的零:
x <- read.table(text=dat, header=TRUE)
x$timestamp <- paste(x$timestamp, "00", sep="")
x$timestamp <- as.POSIXct(x$timestamp, format="%Y-%m-%d %H:%M:%S%z")
x

转换为动物园

library(zoo)
as.zoo(x)
  timestamp           depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom
1 2012-05-24 00:30:12 16.40 17.16                       0.76                              
2 2012-05-24 00:15:08 16.38 17.16                       0.78                              
3 2012-05-24 00:00:03 16.39 17.16                       0.77                              
4 2012-05-23 23:45:13 16.35 17.16                       0.81                              
5 2012-05-23 23:30:08 16.37 17.16                       0.79   

但是这改变了时间序列的值:请查看我的数据中的时间戳值和您获得的时间戳值。 - rockswap
我认为没有。数据显示与UTC相差5小时。Zoo对象打印出了正确的UTC时间。如果您想要不同的行为,可以轻松地使用类似的逻辑从数据显示本地时间中删除“-05”字符串。 - Andrie
好的,谢谢您提供的信息。 我想要计算两个时间值之间的差。我尝试使用这段代码,但是失败了。请问有什么方法可以实现吗? - rockswap
要从 POSIXct 对象中添加(或减去)时间,只需添加(或减去)所需的秒数,例如 z <- date - seconds - Andrie
不是那样的... 在将其转换为动物园后:我得到了数据集x 然后我想-> x [2,1] - x [3,1] 并以小时为单位获得答案。可能吗?这将非常有帮助。 - rockswap
3
@rockswap:那是一个不同的问题;我已经在将xts或zoo时间序列对象的每行除以固定行zoo/xts-无法对1个单元格子集进行数学运算?R挂起上回答了它。 - Joshua Ulrich

4
这适用于提供的帖子数据,只要忽略每个日期/时间末尾的-05。 (要从文件中读取,请使用类似被注释掉的行的内容。)
Lines <- '"timestamp"          "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999'

library(zoo)
# z <- read.zoo("myfile.txt", tz = "")
z <- read.zoo(text = Lines, tz = "")

以上代码的输出结果如下:
> z
                    depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom
2012-05-23 17:30:08 16.37                       17.16                               0.79
2012-05-23 17:45:13 16.35                       17.16                               0.81
2012-05-23 18:00:03 16.39                       17.16                               0.77
2012-05-23 18:15:08 16.38                       17.16                               0.78
2012-05-23 18:30:12 16.40                       17.16                               0.76

如需更多信息,请尝试?read.zoo?read.table,以及vignette("zoo-read")。最后一个链接是一个专门提供read.zoo示例的完整文档。

编辑:添加了评论链接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接