首页 > 解决方案 > 选择一个数字是否包含在一个范围之间

问题描述

refDat <- structure(list(CatMin = c(0L, 17L, 28L, 34L, 48L, 64L, 120L
                    ), CatMax = c(16L, 27L, 33L, 47L, 63L, 119L, 160L), correctionRef = c(0L, 
                    0L, 0L, 0L, -5L, -15L, -40L)), row.names = c(NA, -7L), class = "data.frame")

我想选择 30 位于CatMin&之间的行CatMax。在上面的例子中,它位于第三行

with(refDat, CatMax <= 30 & CatMin >= 30)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

# However, it is returning all the FALSE to me. 

标签: r

解决方案


一种dplyr选择可能是:

refDat %>%
 rowwise() %>%
 filter(between(30, CatMin, CatMax))

  CatMin CatMax correctionRef
   <int>  <int>         <int>
1     28     33             0

或者,您也可以使用between()from data.table,它是矢量化的:

refDat %>%
 filter(data.table::between(30, CatMin, CatMax))

推荐阅读