Conditional subset with two variables at once?

Hello,

I have dataframe df1, how can I keep it's rows only if both Name1 and Name2 appear in df2?

Illustration below:

I've tried subsetting but can't make it work with two variables at once. I want to perform the action, "df3 is df1 but only where df1$Name1 and df1$Name2 both exist as columns of df2.

Thanks!

Try this:

library(dplyr)
name_list <- colnames(df2)
df1 %>% 
  filter(Name1 %in% name_list, Name2 %in% name_list) -> df1_subset

Let me know if it works!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

This worked beautifully. Thanks for the help, Erica!