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

我希望在这个问题上得到帮助。谢谢您的考虑。

4

2 回答 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   
于 2012-05-28T07:40:30.297 回答
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示例。

编辑:添加了评论链接。

于 2012-05-28T10:57:36.893 回答