r - 选择一个数字是否包含在一个范围之间
问题描述
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.
解决方案
一种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))
推荐阅读
- javascript - 使用 Javascript 或 jQuery 拖放时如何交换元素
- apache-spark - 如何将具有相同/共享分区的 HDFS 文件块应用于同一数据节点上的不同文件/表
- angular - IonicRouteStrategy 和 RouteReuseStrategy
- node.js - Heroku 部署和流量
- php - 如何使用 ejabberd 一次性向我名册中的所有朋友发送消息或向我名册中的每个人广播消息?
- bigdata - 无法在 Vespa 的 Document 中动态添加 Postion(纬度和经度)类型字段
- json - 使用 Vue JS 从 API 中获取数据时遇到问题
- r - 排列 R 闪亮的侧边栏对象
- xamarin.android - 安装 Microsoft.Azure.Mobile.Client NuGet 包后生成错误
- java - 监控方法统计