我有一个如下所示的数据框,并且希望按两列中的匹配项进行过滤。
在下面,我想过滤EMILY和FEMALE以及MICHAEL AND MALE。我可以设置某种列表或数据框,然后使用它进行过滤吗?
Name,Year,Gender,Count
Emily,2000,Female,25952
Emily,2001,Female,34294
Michael,2000,Male,345697
Michael,2000,Female,4
Michael,2001,Male,185029
Michael,2001,Female,5
答案 0 :(得分:2)
您确实可以定义一个包含感兴趣案例的数据框,然后使用inner_join
:
toFilter <- data.frame(Name = c("Emily", "Michael"), Gender = c("Female", "Male"))
inner_join(df, toFilter)
Joining, by = c("Name", "Gender")
# Name Year Gender Count
# 1 Emily 2000 Female 25952
# 2 Emily 2001 Female 34294
# 3 Michael 2000 Male 345697
# 4 Michael 2001 Male 185029
答案 1 :(得分:1)
那又怎么样:
library(tidyverse)
df %>%
filter( (Name == "Emily" & Gender == "Female") | (Name == "Michael" & Gender == "Male") )
使用的数据:
df <- read_csv("Name,Year,Gender,Count
Emily,2000,Female,25952
Emily,2001,Female,34294
Michael,2000,Male,345697
Michael,2000,Female,4
Michael,2001,Male,185029
Michael,2001,Female,5")