I have a dataset with groups--"A", "B", "C", and "A & B"--at two time points--"before" and "after". I only want to include "A & B" if the any of the sample sizes for A or B at either time point fall below 10 people. Otherwise, I want to drop the "A & B" group. How do I tell R to drop this group only if the other criteria are satisfied?
Here's are two sample datasets--one where it should filter out group A & B and one where it should retain it:
library(dplyr)
#This should not filter out anything
should_not_drop_group <- tibble(group = rep(c("A", "B", "C", "A & B"), 2),
time = c(rep(c("Before"), 4), rep(c("After"), 4)),
sample_size = c(5, 100, 132, 105, 250, 50, 224, 300))
#This dataset should drop group A&B
should_drop_group <- tibble(group = rep(c("A", "B", "C", "A & B"), 2),
time = c(rep(c("Before"), 4), rep(c("After"), 4)),
sample_size = c(500, 100, 132, 600, 250, 50, 224, 300))
And here's why I tried to no avail:
library(dplyr)
should_drop_group %>%
filter_if(~any(sample_size[group %in% c("A", "B")] < 10), group != "A & B" )