You can do this sort of thing
library(tidyverse)
set.seed(42)
example_df <- tibble(
id = sample.int(1000,1000,replace=TRUE),
drug = paste0(sample(letters,size = 1000,replace=TRUE),sample.int(2,1000,replace=TRUE))
)
# look at common drugs...
table(example_df$drug) %>% sort(decreasing = TRUE) %>% head
#composing the function we want to use
find_ids_with_2_drugs <- function(df,drug_1,drug_2){
df_1 <- filter(df,
drug==drug_1)
df_2 <- filter(df,
drug==drug_2)
inner_join(df_1,
df_2,
by="id")
}
#using it
find_ids_with_2_drugs(example_df,"j1","s1")
# result gives 138 as ameeting the condition
#verify 138
filter(example_df,
id==138)