【问题标题】:subset by vector in rr中的向量子集
【发布时间】:2014-03-26 20:40:36
【问题描述】:

我正在尝试使用向量对 OHLC 每小时数据的 xts 对象进行子集化。

如果我使用以下命令自己创建向量

lookup = c("2012-01-12", "2012-01-31", "2012-03-05", "2012-03-19")
testdfx[lookup]
testdfx[lookup]

我得到了显示的正确数据,其中显示了与向量中的日期匹配的所有时间(00:00 到 23:00。

> head(testdfx[lookup])
                       open    high     low   close
2012-01-12 00:00:00 1.27081 1.27217 1.27063 1.27211
2012-01-12 01:00:00 1.27212 1.27216 1.27089 1.27119
2012-01-12 02:00:00 1.27118 1.27166 1.27017 1.27133
2012-01-12 03:00:00 1.27134 1.27272 1.27133 1.27261
2012-01-12 04:00:00 1.27260 1.27262 1.27141 1.27183
2012-01-12 05:00:00 1.27183 1.27230 1.27145 1.27165

> tail(testdfx[lookup])
                       open    high     low   close
2012-03-19 18:00:00 1.32451 1.32554 1.32386 1.32414
2012-03-19 19:00:00 1.32417 1.32465 1.32331 1.32372
2012-03-19 20:00:00 1.32373 1.32415 1.32340 1.32372
2012-03-19 21:00:00 1.32373 1.32461 1.32366 1.32376
2012-03-19 22:00:00 1.32377 1.32424 1.32359 1.32366
2012-03-19 23:00:00 1.32364 1.32406 1.32333 1.32336

但是,当我从对象中提取日期并创建用于子集的向量时,我的子集中只会显示 00:00-19:00 的时间。

> head(testdfx[dates])
                      open   high    low  close
2007-01-05 00:00:00 1.3092 1.3093 1.3085 1.3088
2007-01-05 01:00:00 1.3087 1.3092 1.3075 1.3078
2007-01-05 02:00:00 1.3079 1.3091 1.3078 1.3084
2007-01-05 03:00:00 1.3083 1.3084 1.3073 1.3074
2007-01-05 04:00:00 1.3073 1.3080 1.3061 1.3071
2007-01-05 05:00:00 1.3070 1.3072 1.3064 1.3069

> tail(euro[nfp.releases])
                       open    high     low   close
2014-01-10 14:00:00 1.35892 1.36625 1.35728 1.36366
2014-01-10 15:00:00 1.36365 1.36784 1.36241 1.36743
2014-01-10 16:00:00 1.36742 1.36866 1.36693 1.36719
2014-01-10 17:00:00 1.36720 1.36752 1.36579 1.36617
2014-01-10 18:00:00 1.36617 1.36663 1.36559 1.36624
2014-01-10 19:00:00 1.36630 1.36717 1.36585 1.36702

我比较了两个包含所需日期的对象,它们似乎是相同的。

> class(lookup)
[1] "character"
> class(nfp.releases)
[1] "character"
> str(lookup)
 chr [1:4] "2012-01-12" "2012-01-31" "2012-03-05" "2012-03-19"
> str(nfp.releases)
 chr [1:86] "2014-02-07" "2014-01-10" "2013-12-06" "2013-11-08" ..

我是 R 新手,但在过去的 3 天里我尝试了一切以使其正常工作。如果我不能这样做,我最终将不得不手动创建一个变量,但由于它有 86 个日期,这可能需要一些时间。

提前致谢。

【问题讨论】:

  • a) 你是如何创建euro 的? b) 您是否知道原始euro 数据中日期2014-01-10 存在超过19:00:00 的时间?

标签: r date vector subset


【解决方案1】:

我无法重现您的问题

lookup = c("2012-01-12", "2012-01-31", "2012-03-05", "2012-03-19")

time_index <- seq(from = as.POSIXct("2012-01-01 07:00"),  to = as.POSIXct("2012-05-17 18:00"), by = "hour")

set.seed(1)

value <- matrix(rnorm(n = 4*length(time_index)),length(time_index),4)

testdfx <- xts(value, order.by = time_index)

testdfx[lookup[1]]

testdfx["2012-01-12"]

【讨论】:

    【解决方案2】:

    感谢大家的回复,我实际上以为我已经删除了这个帖子,但显然没有。

    上述案例中的问题是在距计算机 3 英尺左右的地方发现的。查看数据时,我只对周五感兴趣,这也意味着外汇市场在周末关闭。

    很抱歉浪费了您的时间,请管理员删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2021-06-16
      相关资源
      最近更新 更多