根据每组中不同值的数量进行过滤 [英] Filter based on number of distinct values per group
本文介绍了根据每组中不同值的数量进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下df:
names sex
adam M
jill F
stewart M
jordan M
alica F
jordan F
我该如何过滤行,以便仅获得名称为M和F的名称,在本例中为jordan。
How do I filter the rows so that I only get the names that are both M and F, in this case, jordan.
推荐答案
我们可以按名称分组并按过滤器
'具有个唯一
元素数量大于1的
We can group by 'names' and filter
the 'sex' having unique
number of elements greater than 1
library(dplyr)
df %>%
group_by(names) %>%
filter(n_distinct(sex) > 1)
或者另一个选择是按名称和过滤器
分组具有 M和 F的组
Or another option is to group by 'names' and filter
the groups having both the 'M' and 'F'
df %>%
group_by(names) %>%
filter(all(c("M", "F") %in% sex))
这篇关于根据每组中不同值的数量进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文