Home > Net >  How to use 2 columns in another table to filter simultaneously in dplyr
How to use 2 columns in another table to filter simultaneously in dplyr

Time:10-20

I am trying to do something of this sort. I have 2 tables that would look like this:

Table 1

col1 col2 col3
One 1 A
Three 3 C

Table 2

col1 col2 col3
One 1 A
Two 2 B
Three 3 C

Then I would run this code to filter the 2 table.

final_table <- table_2 %>%
   filter(col1 %in% table_1$col1 &
          col2 %in% table_1$col2)

My understanding was it looked at 'table_1$col1' and 'table_1$col2' where they matched and filtered table_2 whenever the rows have those 2 columns matching.

The result would be:

Table 2

col1 col2 col3
One 1 A
Three 3 C

I guess the question is, is this a correct way of thinking?

CodePudding user response:

We may do an inner_join

library(dplyr)
inner_join(table_1, table_2)
  • Related