简体   繁体   中英

Filtering using dplyr filter() on multiple conditions

I have a dataframe containing unique values of two variables:

df <- data.frame(V1=LETTERS,V2=c(1:26))

I'd like to filter another dataframe for values in df$V1 and corresponding value of df$V2 . This is what I have tried which obviously doesn't produce the desired result:

df2 <- data.frame(V1=c('A','A','B','B','A'),
                    V2=c(1,2,2,3,4))
df2 %>% filter(V1 %in% unique(df$V1) & V2 %in% unique(df$V2))

The result I am expecting post filtering is:

  V1 V2
1  A  1
2  B  2

How do I achieve this?

merge(df,df2)
  V1 V2
1  A  1
2  B  2

library(tidyverse)
inner_join(df,df2)
  V1 V2
1  A  1
2  B  2

You can also use

df[df$V1 %in% unique(df2$V1),]

#   V1 V2
# 1  A  1
# 2  B  2

or

library(tidyverse)

df %>% filter(V1 %in% unique(df2$V1))

#   V1 V2
# 1  A  1
# 2  B  2

In both cases above, you'll get the rows of df where V1 of df matches the (unique) values of V1 of df2 .

Another option is intersect

library(dplyr)
intersect(df, df2)
#  V1 V2
#1  A  1
#2  B  2

Or using data.table (assuming both are data.table objects and have the same attributes)

library(data.table)
fintersect(df, df2)
#   V1 V2
#1:  A  1
#2:  B  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