我有这个时间序列数据:
"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
我希望得到有关此问题的帮助。 谢谢你的考虑。
答案 0 :(得分: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
答案 1 :(得分: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
示例的整个文档。
编辑:添加了评论链接。