简体   繁体   中英

How to use 2 columns in another table to filter simultaneously in dplyr

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?

We may do an inner_join

library(dplyr)
inner_join(table_1, table_2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM