2017-01-29 140 views
0

有没有简单的方法来评估一个范围,并检查一个整数是否在该范围内?检查一个整数是否在data.table的特定范围内?

此职位以外Check to see if a value is within a range in R?我没有找到其他相关的。

range <- cut(rep(1,5),4) # Create intervals 
range.test <- range[2] 
# Now I want to check whether integer 1L is within the range.test (Of course it is) 
Code comes here. 

我试图使用findInterval并且还转换range.test到载体,或使用seqinrange或其它功能,但是失败了。

由于所有分析都基于data.table,并且这部分分析构成了整个实践的一部分,其输出优选为一个data.table,所以我将标记data.table用于确保一致性。

EDIT

data.table上下文中的全貌。

dt <- data.table(structure(list(Time = c("2016-01-04 09:05:06", "2016-01-04 09:20:00","2016-01-04 09:30:00", "2016-01-04 09:30:01", "2016-01-04 09:30:02","2016-01-04 09:30:05", "2016-01-04 09:30:06", "2016-01-04 09:31:35","2016-01-04 09:31:38", "2016-01-04 09:32:33"), Price = c(105,104.1, 104.1, 103.9, 104.1, 104, 104.1, 104.1, 104.1, 104), Volume = c(9500L,23500L, 18500L, 12500L, 16118L, 13000L, 2500L, 300L, 500L, 500L), Flag = c(1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L), Ticker = c("0001","0001", "0001", "0001", "0001", "0001", "0001", "0001", "0001","0001")), .Names = c("Time", "Price", "Volume", "Flag", "Ticker"), class = c("data.table", "data.frame"), row.names = c(NA, -10L))) 
 
        Time Price Volume Flag Ticker 
1: 2016-01-04 09:05:06 105.0 9500 1 0001 
2: 2016-01-04 09:20:00 104.1 23500 0 0001 
3: 2016-01-04 09:30:00 104.1 18500 1 0001 
4: 2016-01-04 09:30:01 103.9 12500 0 0001 
5: 2016-01-04 09:30:02 104.1 16118 1 0001 
6: 2016-01-04 09:30:05 104.0 13000 0 0001 
7: 2016-01-04 09:30:06 104.1 2500 1 0001 
8: 2016-01-04 09:30:07 104.1 1500 1 0001 
9: 2016-01-04 09:30:08 104.3 500 1 0001 
10: 2016-01-04 09:30:10 104.0 1000 0 0001 
11: 2016-01-04 09:30:11 103.9 1000 0 0001 
12: 2016-01-04 09:30:15 104.0 3500 1 0001 
13: 2016-01-04 09:30:17 104.3 2000 1 0001 
14: 2016-01-04 09:30:19 104.3 1500 1 0001 
15: 2016-01-04 09:30:20 104.4 500 1 0001 
16: 2016-01-04 09:30:21 104.4 1500 1 0001 
17: 2016-01-04 09:30:22 104.4 1000 1 0001 
18: 2016-01-04 09:30:24 104.4 1500 1 0001 
19: 2016-01-04 09:30:25 104.0 2000 0 0001 
20: 2016-01-04 09:30:27 104.1 3500 1 0001 
21: 2016-01-04 09:30:35 104.0 500 0 0001 
22: 2016-01-04 09:31:14 104.1 5000 1 0001 
23: 2016-01-04 09:31:15 104.1 500 1 0001 
24: 2016-01-04 09:31:18 104.1 2500 1 0001 
25: 2016-01-04 09:31:25 104.1 3000 1 0001 
26: 2016-01-04 09:31:29 104.0 2000 0 0001 
27: 2016-01-04 09:31:30 104.1 500 1 0001 
28: 2016-01-04 09:31:35 104.1 300 1 0001 
29: 2016-01-04 09:31:38 104.1 500 1 0001 
30: 2016-01-04 09:32:33 104.0 500 0 0001 

# First get the distribution of the Volume 
    distribution <- dt[Flag == 1, sum(Volume), by = cut(Price, 5)][, percentage := list(V1/sum(V1))] 
# Get the max range bin 
Max_range <- distribution[which.max(percentage), cut] 
# Get the Closing price 
Closing_price <- dt[.N, Price] 
# Check whether the closing price is in the Max_range 
Code comes here[?????] 

所以来这里的问题:针对特定Ticker,如何检查的收盘价是否在规定的范围内?只需要TrueFalse即可。如果closing_priceMax_range内,则对应的Signal将是True,否则将是False

EDIT 2

添加所需的输出

所需的输出

 
    Ticker Signal 
1: 0001 False 

所以我想创建一个函数来检查信号是否TrueFalse,然后更新在data.table中。

非常感谢!

+0

您所定义的时间间隔的方式只是创建了“间隔”的一个因素的字符串,水平。间隔的'findInterval'输入是一个非递减的数字向量。 – Naumz

+0

如果你想要一个'data.table'测试的答案,你应该发布构建一个有用的测试用例的代码。 –

+0

你好,@Naumz,我编辑了原文,说清楚了。你能否提供任何提示?谢谢! – Bigchao

回答

0

range.test对象是与levels(range.test)一个因子可变:

levels(range.test) 
[1] "(0.999,0.9995]" "(0.9995,1]"  "(1,1.0005]"  "(1.0005,1.001]" 

当它传递给findInterval因为它被强制为2的数值的第二个参数,所以这是结果:

> findInterval(1,2) 
[1] 0 

这是应该发生的原因,因为1小于2.如果你真的想要一个从0.999到1.001的数值序列,并且有5个值,你可以使用seq:

> seq(0.999, 1.001, length=5) 
[1] 0.9990 0.9995 1.0000 1.0005 1.0010 

然后,您可以测试该矢量的哪个区间数为1。000会撒谎:

> findInterval(1, seq(0.999, 1.001, length=5)) 
[1] 3 
+0

非常感谢@ 42-,我已经对原文进行了更新以明确。你能否提供任何指示?非常感谢! – Bigchao

1

所以我理解正确的话,你要找到每个股票代码(001,002等),如果存在处于给定范围之外的值?

如果是这样的问题,你可以从dplyr和逻辑表达式中使用GROUP_BY功能:

group_by(dt,Ticker) %>% 
    summarise(Signal=any(with(.,Price>max_price & Price<min_price))) 
相关问题