r - conditional matching between variables in dplyr
问题描述
I am trying to find observations within a column that have certain or all the possible values within another column. In this tibble
parties <- tibble(class = c("R","R","R","R","R","K","K","K","K","K","K",
"L","L","L","L"),
name = c("Party1", "Party2","Party3","Party4","Party5",
"Party2", "Party4", "Party6","Party7","Party8","Party9",
"Party2","Party3","Party4","Party10"))
I want to find all the "parties" that are in all three classes "R", "K" and "L". Or generally parties that are in class "X" or "Y". I managed to find a solution, using group_split(class)
, then extracting each table from the list and then lastly performing two semi_joins. That is for the case when I want parties that are in all three classes
:
parties_split <- parties %>%
group_split(class)
parties_K <- parties_split[[1]]
parties_L <- parties_split[[2]]
parties_R <- parties_split[[3]]
semi_join(parties_K,parties_L, by = "name") %>%
semi_join(parties_R, by = "name") %>%
select(-class)
name
<chr>
Party2
Party4
This would work in this case but would not be efficient especially if the number of classes (or observations) that need to match are much larger than three. I am looking in particular for solutions in tidyverse. Any ideas? Thanks
解决方案
试试看:
parties %>%
group_by(name) %>%
filter("K" %in% class,
"R" %in% class,
"L" %in% class) %>%
summarise()
# A tibble: 2 x 1
name
<chr>
1 Party2
2 Party4
编辑:如果您想与超过 3 方合作,您还可以使用:
mask = c("K", "R", "L")
parties %>%
group_by(name) %>%
filter(all(mask %in% class)) %>%
summarise()
推荐阅读
- javascript - Webpack 找不到某些模块
- c++ - 如何从小数中获取分子和分母?
- r - 如何将 text() 与多行字符串一起使用并从 y 坐标向下绘制字符串?
- spring-boot - 升级到 Springboot 2 AbstractRoutingDataSource 不工作
- python-3.x - 在 python3.5 中禁用第三方模块中的所有资源警告发射
- html - Ditty RSS 代码 - Wordpress
- node.js - MongoDB 更新字段数据
- c - Fibonacci Sequence C program error
- php - PHP如何使用form-datalist在db中输入数据?
- angular - 在 Kendo UI 中导出受保护的 PDF 文件