如何使用 R 管道对数据框进行子集化

How to subset a data frame with R pipeline

我正在尝试 subset/filter 一个数据框根据另一个数据框的相应列元素。 这是我以前做的

df <- df1[df1$col1 %in% df2$col2,]

然后我要将列设置为行名称

df <- df %>% remove_rownames %>% column_to_rownames('col1')

但是我不知道如何使用 %>%

将这两个代码合二为一
df1 %>% filter(col1 %in% df2$col2) %>% remove_rownames %>% column_to_rownames('col1')