在dplyr中,setdiff和anti_join之间的内在差异是什么?

时间:2017-10-20 17:10:47

标签: r dplyr

我还在为DataCamp for R上课,所以如果这个问题看起来很天真,请原谅我。

考虑以下(非常人为的)样本:

library(dplyr)
library(tibble)

type <- c("Dog", "Cat", "Cat", "Cat")
name <- c("Ella", "Arrow", "Gabby", "Eddie")
pets = tibble(name, type)

name <- c("Ella", "Arrow", "Dog")
type <- c("Dog", "Cat", "Calvin")
favorites = tibble(name, type)

anti_join(favorites, pets, by = "name")
setdiff(favorites, pets, by = "name")

这两个都将完全返回相同的数据:

> anti_join(favorites, pets, by = "name")
# A tibble: 1 × 2
   name   type
  <chr>  <chr>
1   Dog Calvin

> setdiff(favorites, pets, by = "name")
# A tibble: 1 × 2
   name   type
  <chr>  <chr>
1   Dog Calvin

每个文档的文档似乎只表示一个微妙的区别:setdiff返回行,但anti_join没有。从我的测试来看,情况似乎并非如此。

有人可以向我解释这两者之间的真正差异,也许可以提供一个更清楚地说明差异的更好的例子吗? (这是DataCamp没有特别帮助的领域。)

2 个答案:

答案 0 :(得分:7)

两个子集都是第一个参数,但setdiff要求列相同:

library(dplyr)

setdiff(mtcars, mtcars[1:30, ])
#>    mpg cyl disp  hp drat   wt qsec vs am gear carb
#> 1 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
#> 2 21.4   4  121 109 4.11 2.78 18.6  1  1    4    2

setdiff(mtcars, mtcars[1:30, 1:6])
#> Error in setdiff_data_frame(x, y): not compatible: Cols in x but not y: `carb`, `gear`, `am`, `vs`, `qsec`.

anti_join是联接,所以不是:

anti_join(mtcars, mtcars[1:30, 1:3])
#> Joining, by = c("mpg", "cyl", "disp")
#>    mpg cyl disp  hp drat   wt qsec vs am gear carb
#> 1 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
#> 2 21.4   4  121 109 4.11 2.78 18.6  1  1    4    2

答案 1 :(得分:3)

setdiff和anti_join之间的一个区别是你可以在anti_join中选择列。例如:

df1 <- data.frame(a = c(1,3,5,4), b = c(5,6,7,8))
df2 <- data.frame( a = c(1,2,3,4), b = c(5,9,7,8))
#df1 looks like        df2 look like
# a   b                a   b    
# 1   5                1   5
# 3   6                2   9 
# 5   7                3   7
# 4   8                4   8

set_diff(x,y)

#The first and last rows of df1 and df2 are identical. 
#So set_diff(df1,df2) will return the 2nd and 3rd row of df1

#a b
#3 6
#5 7

#If I do the same thing with anti_join, I get the same results
anti_join(df1,df2)

#a b
#3 6
#5 7
#However,...if I only care about values in df1 column 'b' that are different from the 
# corresponding value in column b of df2... I can use the option "by" parameter..

anti_join(df1,df2, by = 'b')

 #Since column the only number in column b of df1 that is different 
#from the corresponding value in df2 is row two, 
#this returns row 2 of df1

#a b
#3 6

另一个区别是在set_diff中,两个数据帧必须具有相同的列。

#Keeping df1 identical to df1 in the previous example... 
# and df2 the same but with an additional column

df1 <- data.frame(a = c(1,3,5,4), b = c(5,6,7,8))
df2 <- data.frame(a = c(1,2,3,4), b = c(5,9,7,8), l = c(9,9,9,9))

#df1 looks like        df2 look like
# a   b                a   b  c   
# 1   5                1   5  9
# 3   6                2   9  9 
# 5   7                3   7  9
# 4   8                4   8  9

setdiff(df1,df2)
#Returns:
# Error in setdiff_data_frame(x, y) : 
# not compatible: Cols in y but not x: `l`. 

anti_join(df1,df2)
#Ignores column 3 of df2, since there is no corresponding column in df1.  
#Returns: rows in df1 in which (a,b) are not equal to (a,b) in df2 
#(which will be identical to the output when df2 didn't have 
#a third column).

# a b
# 3 6
# 5 7

anti_join(df1,df2, by = 'b')

#Since column the only number in column b of df1 that is different
# from the corresponding value in df2 is row two, this returns row 2 of   df1...
#...same as when df2 only had two columns