【问题标题】:Check whether one integer is within specific range in data.table?检查data.table中的一个整数是否在特定范围内?
【发布时间】:2017-01-29 06:46:03
【问题描述】:

有没有一种简单的方法来评估一个范围并检查一个整数是否在该范围内?

除了这个帖子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以确保一致性。

编辑

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)))
时间价格成交量标志代码 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内,则对应的SignalTrue,否则为False

编辑 2

添加了所需的输出

想要的输出

行情信号 1: 0001 错误

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

非常感谢!

【问题讨论】:

  • 您定义区间的方式只是将“区间”创建为字符串、因子的级别。 findInterval for interval 的输入是一个非递减的数字向量。
  • 如果您想要data.table-tested 答案,您应该发布构建有用测试用例的代码。
  • 你好,@Naumz,我编辑了原始帖子以使其清楚。你能提供任何提示吗?谢谢!
  • 你想要的输出是什么?
  • @DavidArenburg,我在帖子中添加了所需的输出。请你看一下。非常感谢!

标签: r data.table


【解决方案1】:

如果有一个值超出给定范围,我是否正确理解您想要为每个代码(001,002 等)查找?

如果这是问题,您可以使用 dplyr 中的 group_by 函数和逻辑表达式:

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

【讨论】:

    【解决方案2】:

    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
    

    【讨论】:

    • 非常感谢@42-,我已经对原始帖子进行了更新以使其清楚。你能提供任何指示吗?非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2021-04-01
    • 2011-06-08
    • 2011-06-29
    • 2011-03-31
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多