anti_join(x, y) 删除 x 中与 y 匹配的所有观测值。
df1 <- data.frame(
stringsAsFactors = FALSE,
Col1 = c("aaa", "tfe", "eed", "wes", "rtf", "fvg"),
Col2 = c(12L, 21L, 12L, 95L, 44L, 87L),
Col3 = c("xxx", "xxx", "xxx", "xxx", "xxx", "xxx"),
Col4 = c("32b", "14f", "54b", "54r", "99q", "55h")
)
df2 <- data.frame(
stringsAsFactors = FALSE,
Col1 = c("a", "tfe", "ee", "ws", "rt", "fvg"),
Col2 = c(12L, 21L, 12L, 95L, 44L, 87L),
Col3 = c("xxx", "xxx", "xxx", "xxx", "xxx", "xxx"),
Col4 = c("32b", "14f", "54b", "54r", "99q", "55h")
)
library(dplyr)
unique1 <- df1 %>%
anti_join(df2)
unique2 <- df2 %>%
anti_join(df1)
# Join them together:
rbind(unique1, unique2)
#> Col1 Col2 Col3 Col4
#> 1 aaa 12 xxx 32b
#> 2 eed 12 xxx 54b
#> 3 wes 95 xxx 54r
#> 4 rtf 44 xxx 99q
#> 5 a 12 xxx 32b
#> 6 ee 12 xxx 54b
#> 7 ws 95 xxx 54r
#> 8 rt 44 xxx 99q
由reprex package (v0.3.0) 于 2021-03-16 创建