根据每组不同值的数量进行过滤 [英] 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.
推荐答案
我们可以按 'names' 和 filter
对具有 unique
元素数量的 'sex' 进行分组比 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)
<小时>
或者另一种选择是按 'names' 和 filter
同时具有 '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屋!
查看全文