Create all possible pairs of two character vectors.

all_pairs(x, y = character(0), sort = FALSE, result = "matrix")

names2pairs(x, y = NULL, sort = TRUE, result = "list")

Arguments

x, y

Character vectors.

sort

Logical.

result

A list or a matrix.

Details

NOTICE: If y is not NULL then x and y must be disjoint (no checks are made); otherwise pairs of identical elements wil also be obtained.

Author

Søren Højsgaard, sorenh@math.aau.dk

Examples


x <- letters[1:4]
y <- letters[5:7]

all_pairs(x)
#>      [,1] [,2]
#> [1,] "a"  "b" 
#> [2,] "a"  "c" 
#> [3,] "a"  "d" 
#> [4,] "b"  "c" 
#> [5,] "b"  "d" 
#> [6,] "c"  "d" 
all_pairs(x, result="matrix")
#>      [,1] [,2]
#> [1,] "a"  "b" 
#> [2,] "a"  "c" 
#> [3,] "a"  "d" 
#> [4,] "b"  "c" 
#> [5,] "b"  "d" 
#> [6,] "c"  "d" 

all_pairs(x, y)
#>       [,1] [,2]
#>  [1,] "a"  "e" 
#>  [2,] "a"  "f" 
#>  [3,] "a"  "g" 
#>  [4,] "b"  "e" 
#>  [5,] "b"  "f" 
#>  [6,] "b"  "g" 
#>  [7,] "c"  "e" 
#>  [8,] "c"  "f" 
#>  [9,] "c"  "g" 
#> [10,] "d"  "e" 
#> [11,] "d"  "f" 
#> [12,] "d"  "g" 
all_pairs(x, y, result="matrix")
#>       [,1] [,2]
#>  [1,] "a"  "e" 
#>  [2,] "a"  "f" 
#>  [3,] "a"  "g" 
#>  [4,] "b"  "e" 
#>  [5,] "b"  "f" 
#>  [6,] "b"  "g" 
#>  [7,] "c"  "e" 
#>  [8,] "c"  "f" 
#>  [9,] "c"  "g" 
#> [10,] "d"  "e" 
#> [11,] "d"  "f" 
#> [12,] "d"  "g"