按值的出现过滤组 [英] Filter groups by occurrence of a value
问题描述
如何根据各个行上的条件选择组,例如过滤包含值4(或任何其他条件)的所有组。
How to select groups based on a condition on the individual rows, say filter all groups that contain value 4 (or any other condition).
让我们非常简单数据,有两个组,我想选择组 B (值4)
Let's take a very simple data, with two groups, and I want to select the group B (as has a Value of 4)
library(dplyr)
df <- data.frame(Group=LETTERS[c(1,1,1,2,2,2)], Value=c(1:5,4))
> df
Group Value
1 A 1
2 A 2
3 B 3
4 B 4
执行 group_by()
,然后过滤器
(如此帖子中所述)将仅选择单个行包含值4,而不是整个组:
Doing group_by()
and then filter
(as in this post) will only select individual rows that contains a value of 4, not the whole group:
df %>%
group_by(Group) %>%
filter(Value==4)
Group Value
<fctr> <int>
1 B 4
推荐答案
非常简单:您只需要在 filter
调用中使用 any()
函数。实际上,似乎出现了:
This turns out to be pretty easy: you just need to use the any()
function in the filter
call. Indeed, it appears that:
-
filter(any(...))
的计算结果为group_by()
级别
filter( ...)
的计算结果为 rowwise()
级别,即使前面有 group_by()
。
filter(...)
evaluates at the rowwise()
level, even when preceded by group_by()
.
因此使用:
df %>%
group_by(Group) %>%
filter(any(Value==4))
Group Value
<fctr> <int>
1 B 3
2 B 4
有趣的是,同样出现在mutate ,比较:
Interestingly, the same appear with mutate, compare:
df %>%
group_by(Group) %>%
mutate(check1=any(Value==4),
check2=Value==4)
Group Value check1 check2
<fctr> <int> <lgl> <lgl>
1 A 1 FALSE FALSE
2 A 2 FALSE FALSE
3 B 3 TRUE FALSE
4 B 4 TRUE TRUE
这篇关于按值的出现过滤组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!