This function efficiently generates Cartesian-product-like output where order does not matter. It is loosely equivalent to the following:

  • t = expand.grid(list)

  • t = t[do.call(order, t), ]

  • key = apply(t, 1, function(x) paste0(sort(x), collapse = ""))

  • t[!duplicated(key), ]

comboGrid(..., repetition = TRUE)

Arguments

...

vectors, factors or a list containing these. (See ?expand.grid).

repetition

Logical value indicating whether results should be with or without repetition. The default is TRUE.

Value

If items with different classes are passed, a data frame will be returned, otherwise a matrix will be returned.

Author

Joseph Wood

Examples

## return a matrix
expGridNoOrder = comboGrid(1:5, 3:9, letters[1:5], letters[c(1,4,5,8)])
head(expGridNoOrder)
#>   Var1 Var2 Var3 Var4
#> 1    1    3    a    a
#> 2    1    3    a    d
#> 3    1    3    a    e
#> 4    1    3    a    h
#> 5    1    3    b    a
#> 6    1    3    b    d
tail(expGridNoOrder)
#>     Var1 Var2 Var3 Var4
#> 539    5    9    c    h
#> 540    5    9    d    d
#> 541    5    9    d    e
#> 542    5    9    d    h
#> 543    5    9    e    e
#> 544    5    9    e    h

expGridNoOrderNoRep = comboGrid(1:5, 3:9, letters[1:5],
                                letters[c(1,4,5,8)], repetition = FALSE)

head(expGridNoOrderNoRep)
#>   Var1 Var2 Var3 Var4
#> 1    1    3    a    d
#> 2    1    3    a    e
#> 3    1    3    a    h
#> 4    1    3    b    a
#> 5    1    3    b    d
#> 6    1    3    b    e
tail(expGridNoOrderNoRep)
#>     Var1 Var2 Var3 Var4
#> 401    5    9    c    d
#> 402    5    9    c    e
#> 403    5    9    c    h
#> 404    5    9    d    e
#> 405    5    9    d    h
#> 406    5    9    e    h