如何获取包含两个其他值之间的值的数据框的行? [英] How to get rows of a dataframe that contain values between two other values?
本文介绍了如何获取包含两个其他值之间的值的数据框的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我想在data.frame的列中找到值,这些值在已定义的值范围内:
So I want to find values in a column of a data.frame, which are in range of defined values:
example
[1] 6 2 4 3 5 1
pos
[1] 1 3 2
我现在想获取下面的语句均为TRUE的示例的值,以便我只得到 pos-1 $ c $之间的值c>和
pos +1
:
I now want to get the values of example for which BOTH of the following statements are TRUE, so that I got only values that lie between pos - 1
and pos +1
:
if(example < pos - 1)
if(example > pos + 1)
现在是实际值我的任务在data.frame中。如何提取包含这些值的完整行并构建新的pos
data.frame
。
And now the real values for my task are within a data.frame. How do I extract the complete rows, which contain these values and build a new pos
data.frame
.
该示例的预期输出为:
result
[1] 2 3 1
谢谢!
推荐答案
设置最小和最大阈值,然后将每个元素与它们进行比较
Set min and max thresholds and then compare each element against them
maxind <- max(pos + 1)
minind <- min(pos - 1)
然后
example[sapply(example, function(x) x > minind & x < maxind)]
## [1] 2 3 1
或者类似地,
example[example > minind & example < maxind]
## [1] 2 3 1
或使用 data.table
library(data.table)
example[between(example, minind, maxind, incbounds = FALSE)]
## [1] 2 3 1
这篇关于如何获取包含两个其他值之间的值的数据框的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文