I am searching for a simpler solution to the following problem. Here is my setup:
test <- tibble::tribble(
~group_name, ~id_name, ~varA, ~varB,
\"g
In base R, we can split
id_name
by group_name
find common id's
and then subset
subset(test, id_name %in% Reduce(intersect, split(id_name, group_name)))
# group_name id_name varA varB
# <chr> <chr> <dbl> <chr>
# 1 groupA id_1 1 a
# 2 groupA id_2 4 f
# 3 groupA id_4 6 x
# 4 groupA id_4 6 h
# 5 groupB id_1 2 s
# 6 groupB id_2 13 y
# 7 groupB id_4 14 t
# 8 groupC id_1 3 d
# 9 groupC id_2 7 j
#10 groupC id_4 9 l
Using similar concept in tidyverse
, it would be
library(tidyverse)
test %>%
filter(id_name %in% (test %>%
group_split(group_name) %>%
map(~pull(., id_name)) %>%
reduce(intersect)))
You can tabulate the the occurrence of id_name by group_name:
table(test$group_name,test$id_name)
If id_name is present in every group, we want columns that have all > 0 entris. We can simplify this logic using a combination of >0 and colMeans:
keep = names(which(colMeans(table(test$group_name,test$id_name)>0)==1))
Using this:
test[test$id_name %in% keep,]