[英]Finding unique rows that are NOT between an interval
I'm trying to find a way to filter a data set so that I see only the rows that do NOT have a measurement in a particular interval.我试图找到一种方法来过滤数据集,以便我只看到在特定时间间隔内没有测量值的行。 For some reason my brain is cannot seem to put the logic together.
出于某种原因,我的大脑似乎无法将逻辑放在一起。 I've created an example dataset below to try and explain my thinking
我在下面创建了一个示例数据集来尝试解释我的想法
library(dplyr)
df <- data.frame (id = c(1,1,1,1,1,1,1,1,2,2,2,2,2, 3, 3),
number = c(-10, -9, -8, -1, -0.5, 0.0, 0.23, 5, -2, -1.1, -.88, 1.2, 4, -10,10))
)
df
So here, ideally, I want to find the unique id's that do NOT have values in between -1 and 0. ID 1 and ID 2 both have values in between -1 and 0, so they would not be included.所以在这里,理想情况下,我想找到值不在 -1 和 0 之间的唯一 ID。ID 1 和 ID 2 的值都在 -1 和 0 之间,因此它们不会被包括在内。
df %>% filter(between(number, -1, 0))
But ID 3 only has measurements of -10 and 10, so that ID does not have measures in between the interval of -1 to 0. I'm trying to get that as my final output (the 2 rows with ID 3).但是 ID 3 只有 -10 和 10 的测量值,因此 ID 在 -1 到 0 的间隔之间没有测量值。我试图将其作为我的最终 output(ID 为 3 的 2 行)。 But can't think of a way to achieve that.
但是想不出一种方法来实现这一目标。
Thanks in advance!提前致谢!
df %>% group_by(id) %>% filter(!any(between(number, -1, 0)))
You could use group_by
and filter
the groups with all
values not in specific range like this:您可以使用
group_by
并使用不在特定范围内的all
值filter
组,如下所示:
library(dplyr)
df <- data.frame (id = c(1,1,1,1,1,1,1,1,2,2,2,2,2, 3, 3),
number = c(-10, -9, -8, -1, -0.5, 0.0, 0.23, 5, -2, -1.1, -.88, 1.2, 4, -10,10))
df %>%
group_by(id) %>%
filter(all(!between(number, -1, 0)))
#> # A tibble: 2 × 2
#> # Groups: id [1]
#> id number
#> <dbl> <dbl>
#> 1 3 -10
#> 2 3 10
Created on 2022-09-30 with reprex v2.0.2创建于 2022-09-30,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.