Skip to contents

[Stable]

Provides alternative function to base::union(), base::intersect(), and base::setdiff().

  • set_union(): Returns the union of the sets in ....

  • set_intersect(): Returns the intersect of the sets in ....

  • set_difference(): Returns the difference of the sets in ....

Usage

set_intersect(..., pairs = FALSE)

set_union(..., pairs = FALSE)

set_difference(..., pairs = FALSE)

Arguments

...

A list or a comma-separated list of vectors in the same class. If vector contains duplicates they will be discarded. If the list doesn't have names the sets will be named as "set_1", "Set_2", "Set_3" and so on. If vectors are given in ..., the set names will be named with the names of the objects provided. Data frames are also allowed, provided that common column names exist.

pairs

Returns the pairwise unions of the sets? Defaults to FALSE.

Value

A vector showing the desired operation of the sets. If pairs = TRUE, returns a list showing the pairwise operation of the sets.

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

# \donttest{
library(metan)
(A <- letters[1:4])
#> [1] "a" "b" "c" "d"
(B <- letters[2:5])
#> [1] "b" "c" "d" "e"
(C <- letters[3:7])
#> [1] "c" "d" "e" "f" "g"

set_union(A, B)
#> [1] "a" "b" "c" "d" "e"
set_intersect(A, B, C)
#> [1] "c" "d"
set_difference(B, C)
#> [1] "b"

# Operations with data frames
# Add a row id for better understanding
sets <- data_ge %>% add_row_id()

set_1 <- sets[1:5,]
set_2 <- sets[2:6,]
set_3 <- sets[3:7,]

set_intersect(set_1, set_2, set_3)
#> # A tibble: 3 × 6
#>   row_id ENV   GEN   REP      GY    HM
#>    <int> <fct> <fct> <fct> <dbl> <dbl>
#> 1      3 E1    G1    3      2.43  47.8
#> 2      4 E1    G2    1      3.21  45.2
#> 3      5 E1    G2    2      2.93  45.3
set_difference(set_2, set_3)
#> # A tibble: 1 × 6
#>   row_id ENV   GEN   REP      GY    HM
#>    <int> <fct> <fct> <fct> <dbl> <dbl>
#> 1      2 E1    G1    2      2.50  46.9
set_union(set_1, set_2, set_3)
#> # A tibble: 7 × 6
#>   row_id ENV   GEN   REP      GY    HM
#>    <int> <fct> <fct> <fct> <dbl> <dbl>
#> 1      1 E1    G1    1      2.17  44.9
#> 2      2 E1    G1    2      2.50  46.9
#> 3      3 E1    G1    3      2.43  47.8
#> 4      4 E1    G2    1      3.21  45.2
#> 5      5 E1    G2    2      2.93  45.3
#> 6      6 E1    G2    3      2.56  45.5
#> 7      7 E1    G3    1      2.77  46.7
# }