【问题标题】:Index error after calling transform/as.xts调用 transform/as.xts 后出现索引错误
【发布时间】:2012-02-27 22:15:07
【问题描述】:

我有一个 xts 数字矩阵,我需要在其上应用许多转换。根据thread,transform() 返回的对象应该包含在对 as.xts() 的调用中(xts 没有自己的转换版本,zoo 返回一个新对象)。

我已经尝试在一些示例数据上进行转换,它似乎工作正常,但是当我在我自己的数据上运行它时,我收到了这个错误:

Browse[2]> class(myxts)
[1] "xts" "zoo"
Browse[2]> mode(myxts)
[1] "numeric"
Browse[2]> str(myxts)
An 'xts' object from 2011-07-22 09:30:00 to 2011-12-19 16:00:00 containing:
  Data: num [1:11606, 1:19] 0 158300 157700 157600 157900 ...
  - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:19] "Open" "High" "Low" "Close" ...
  Indexed by objects of class: [POSIXlt,POSIXt] TZ: 
  xts Attributes:  
 NULL
Browse[2]> head(myxts['2011-07-22'])
                      Open   High    Low  Close
2011-07-22 09:30:00      0      0      0      0      
2011-07-22 09:31:00 158300 158400 157600 157800     
2011-07-22 09:32:00 157700 157700 157500 157700 
2011-07-22 09:33:00 157600 157900 157599 157900
2011-07-22 09:34:00 157900 158100 157800 158100
2011-07-22 09:35:00 158000 158400 157900 158200 
Browse[2]> n
debug: myxts = as.xts(transform(myxts, Open = ifelse(Open == 
    0, NA, Open), Close = ifelse(Close == 0, NA, Close), High = ifelse(High == 
    0, NA, High), Low = ifelse(Low == 0, NA, Low)))
Browse[2]> class(myxts)
[1] "xts" "zoo"
Browse[2]> head(myxts['2011-07-22'])
Error in function (year = 1970, month = 1, day = 1, hour = 0, min = 0,  : 
  unused argument(s) (tz1 = "", tz2 = "EST", tz3 = "EDT")
Browse[2]> str(myxts)
An 'xts' object from 2011-07-22 09:30:00 to 2011-12-19 16:00:00 containing:
  Data: num [1:11606, 1:19] NA 158300 157700 157600 157900 ...
  - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:19] "Open" "High" "Low" "Close" ...
   Indexed by objects of class: [POSIXlt,POSIXt] TZ: 
  TZ: EST
  TZ: EDT
  xts Attributes:  
 NULL

注意

在调用 transform() 后,str(myxts) 的输出中添加了 2 个 TZ: EST/EDT 字段。

【问题讨论】:

    标签: r transform xts


    【解决方案1】:

    我认为问题在于 as.xts() 和 xts() 设置的 TZ 索引不匹配。我的 TZ 环境变量未设置,原始 xts 对象是在没有特定 TZ 值的情况下创建的,如 str() 报告的那样。

    但是,在调用 transform() 和 as.xts() 之后,TZ 索引设置为 EST/EDT。 as.xts() 可能从 env 变量以外的其他来源获取时区。

    通过将 TZ 索引强制为空字符串(或任何 TZ env 变量值),我能够恢复 as.xts() 对象中的索引选择:

    indexTZ(myxts) = Sys.getenv("TZ")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 2020-01-14
      • 2018-12-06
      • 2021-06-14
      • 1970-01-01
      相关资源
      最近更新 更多