简体   繁体   English

r中的向量子集

[英]subset by vector in r

I am trying to subset an xts object of OHLC hourly data with a vector. 我试图用向量子集OHLC每小时数据的xts对象。

If i create the vector myself with the following command 如果我使用以下命令自己创建矢量

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

I get the correct data displayed which shows all the hours that match the dates in the vector (00:00 to 23:00. 我得到正确显示的数据,该数据显示与矢量中的日期(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

However when I extract a dates from an object and create a vector to use for subsetting I only get the hours of 00:00-19:00 displayed in my subset. 但是,当我从对象中提取日期并创建用于子集的向量时,我只能在子集中显示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

I have compared both objects containing the require dates and they appear to be the same. 我已经比较了两个包含所需日期的对象,它们看起来是相同的。

> 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" ..

I am new to R but have tried everything over the past 3 days to get this to work. 我是R的新手,但在过去3天内尝试了所有方法以使其正常工作。 If I can't to it this way I will end up having to create a variable by hand but as its got 86 dates this may take some time. 如果我不能这样做,最终将不得不手工创建一个变量,但是由于它有86个日期,这可能需要一些时间。

Thanks in advance. 提前致谢。

I cannot reproduce your problem 我无法重现你的问题

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"]

Thanks for the response guys I actually thought i had deleted this thread but obviously not. 感谢您的答复,我实际上以为我已经删除了该线程,但显然没有。

The problem in the case above was to be found around 3' from the computer. 在上述情况下,问题出在计算机附近3'处。 When looking through the data I was only interested in Fridays which also means that the FX market is closing down for the week end. 当查看数据时,我只对星期五感兴趣,这也意味着外汇市场本周末将休市。

Sorry to have wasted your time and Admin please remove. 很抱歉浪费了您的时间,管理员请删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM