簡體   English   中英

在dplyr中,setdiff和anti_join之間的內在差異是什么?

[英]In dplyr, what are the intrinsic differences between setdiff and anti_join?

我還在為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沒有特別幫助的領域。)

兩者都是第一個參數的子集,但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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM