【问题标题】:Keeping combinations that contain specific range of values保留包含特定值范围的组合
【发布时间】:2019-03-18 15:55:48
【问题描述】:

我想保留包含 1:30 的 8 个值、31:60 的 1 或 2 个值以及 61:70 的 3 个值的组合,

我有以下组合:

15 6 10 26 7 27 19 51 54 61 64 69 70 
# do not keep this b/c there are 4 values from 61:70

23 2 7 29 3 17 4 20 60 56 61 66 68 # keep this one

17 30 24 3 25 5 15 11 43 49 66 67 68 # keep this one

25 13 14 9 29 16 15 4 56 63 66 67 70 
# do not keep this b/c there are 4 values from 61:70

14 24 3 17 11 15 27 25 31 59 62 65 69

20 28 8 24 1 18 25 3 44 45 69 61 70

... (32 in totals)

我该怎么做? 编辑。

【问题讨论】:

    标签: r


    【解决方案1】:

    我不确定您想如何“保留”所需的组合,但要找到您正在寻找的组合,您可以执行类似的操作

    v <- c(15,6,10,26,7,27,19,51,54,61,64,69,70)
    
    if(sum(v>=1 & v<= 30) == 8 &
       sum(v>=31 & v<= 60) %in% c(1L, 2L) &
       sum(v>=61 & v<= 70) == 3){TRUE}
    else{FALSE}
    

    感谢@thelatemail 指出第二个条件应该接受多个值。

    【讨论】:

    • 关闭,但我认为第二个范围需要是%in% c(1L, 2L)
    • @thelatemail 谢谢!但我认为他是对的,因为我做了一些编辑
    • 但是,c(1L, 2L) 中的 L 是什么?
    • @Mathygroupy - 我不认为他是对的,因为如果你想要 1 或 2 个值,你需要测试,而不仅仅是 ==2L 用于整数。当您比较计数时,1L 正好是其中之一。
    • @thelatemail 是的,我同意。我已经编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2016-11-22
    • 2013-07-13
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多