Wrappers of set operations in data.table. Only difference is it could be applied to non-data.table data frames by recognizing and coercing them to data.table automatically.

intersect_dt(x, y, all = FALSE)

union_dt(x, y, all = FALSE)

setdiff_dt(x, y, all = FALSE)

setequal_dt(x, y, all = TRUE)

Arguments

x

A data.frame

y

A data.frame

all

Logical. When FALSE (default), removes duplicate rows on the result.

Value

A data.table

See also

Examples


x = iris[c(2,3,3,4),]
x2 = iris[2:4,]
y = iris[c(3:5),]

intersect_dt(x, y)            # intersect
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.7         3.2          1.3         0.2  setosa
#> 2:          4.6         3.1          1.5         0.2  setosa
intersect_dt(x, y, all=TRUE)  # intersect all
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.7         3.2          1.3         0.2  setosa
#> 2:          4.6         3.1          1.5         0.2  setosa
setdiff_dt(x, y)              # except
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.9           3          1.4         0.2  setosa
setdiff_dt(x, y, all=TRUE)    # except all
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.9         3.0          1.4         0.2  setosa
#> 2:          4.7         3.2          1.3         0.2  setosa
union_dt(x, y)                # union
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.9         3.0          1.4         0.2  setosa
#> 2:          4.7         3.2          1.3         0.2  setosa
#> 3:          4.6         3.1          1.5         0.2  setosa
#> 4:          5.0         3.6          1.4         0.2  setosa
union_dt(x, y, all=TRUE)      # union all
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.9         3.0          1.4         0.2  setosa
#> 2:          4.7         3.2          1.3         0.2  setosa
#> 3:          4.7         3.2          1.3         0.2  setosa
#> 4:          4.6         3.1          1.5         0.2  setosa
#> 5:          4.7         3.2          1.3         0.2  setosa
#> 6:          4.6         3.1          1.5         0.2  setosa
#> 7:          5.0         3.6          1.4         0.2  setosa
setequal_dt(x, x2, all=FALSE) # setequal
#> [1] TRUE
setequal_dt(x, x2)            # setequal all
#> [1] FALSE