一个相对简单的问题,但我似乎无法找到任何例子。
我有一个简单的外汇价格数据,它位于一个名为subx1的2列xts对象中:
Datetime, Price
2016-09-01 00:00:01, 1.11563
2016-09-01 00:00:01, 1.11564
2016-09-01 00:00:02, 1.11564
2016-09-01 00:00:03, 1.11565
......等等。
我试图在下午2点之后第一次找到价格高于下午2点之前的高点,这是在另一个对象的栏目中名为daypeakxts $ before2.High和
天文字样本是:
Date, before2.High
2016-09-01, 1.11567
2016-09-02, 1.11987
这是我尝试做的一个不好的例子:
subxresult <- index(subx1, subx1$datetime > daypeakxts$before2.High)
...所以我希望在另一个xts对象中使用带有日期值的条件语句来查找价格的日期时间。
答案 0 :(得分:2)
您没有为ismember
提供足够的数据,因此我将使用xts包附带的一些日常数据。
library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix, dateForamt = "Date")
# Aggregate and find the high for each week
Week.High <- apply.weekly(x, function(x) max(x$High))
# Finding the pre-2pm high would be something like:
# Pre.2pm.High <- apply.daily(x["T00:00/T14:00"], function(x) max(x$High))
# Merge the period high with the original data, and
# fill NA with the last observation carried forward
y <- merge(x, Week.High, fill = na.locf)
# Lag the period high, so it aligns with the following period
y$Week.High <- lag(y$Week.High)
# Find the first instance where the next period's high
# is higher than the previous period's high
y$First.Higher <- apply.weekly(y, function(x) which(x$High > x$Week.High)[1])