【问题标题】:how to read time series in xts?如何在 xts 中读取时间序列?
【发布时间】:2012-05-28 07:11:10
【问题描述】:

我有这个时间序列数据:

     "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”位是什么?
  • @BenBolker 正是如此。这就是导致问题的原因,因为as.POSIXct 不知道如何处理。
  • @BenBolker 这似乎是一个格式错误的时间戳。看我的回答。
  • 我不知道它是什么。当我从互联网上获取数据时,它就出现了。我们可以处理它吗?

标签: r time-series xts


【解决方案1】:

这适用于帖子中的数据,前提是可以忽略每个日期/时间结束时的-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 示例。

编辑:添加评论链接。

【讨论】:

    【解决方案2】:

    您的时间戳数据包含格式错误的时区数据,即每个时间戳的 -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   
    

    【讨论】:

    • 但这改变了时间序列值:查看我的数据中的时间戳值和您获得的值
    • 我认为没有。数据表明与 UTC 有 5 小时的偏移。 zoo 对象打印 UTC 时间,这是正确的。如果您想要不同的行为,您可以轻松地使用类似的逻辑从本地时间的数据显示中删除 -05 字符串。
    • 好的..谢谢你。我想减去两个时间值。我尝试使用此代码进行操作但失败了。怎么可能?
    • 要从POSIXct 对象中添加(或减去)时间,只需添加(或减去)您想要的秒数,即z &lt;- date - seconds
    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 2012-03-11
    • 1970-01-01
    • 2019-07-05
    • 2014-09-29
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多