Skip to contents

Same as expand.grid but with stringsAsFactors = FALSE by default and with the values of the first argument being changed last, not first.

Usage

expandGrid(..., stringsAsFactors = FALSE)

Arguments

...

arguments passed to expand.grid, but in reversed order

stringsAsFactors

passed to expand.grid

Examples

persons <- c("Peter", "Paul", "Mary")
fruits <- c("apple", "pear")

# With expand.grid() the values of the first argument change first...
(grid_1 <- expand.grid(person = persons, fruit = fruits))
#>   person fruit
#> 1  Peter apple
#> 2   Paul apple
#> 3   Mary apple
#> 4  Peter  pear
#> 5   Paul  pear
#> 6   Mary  pear

#... with expandGrid() they change last.
(grid_2 <- expandGrid(person = persons, fruit = fruits))
#>   person fruit
#> 1  Peter apple
#> 2  Peter  pear
#> 3   Paul apple
#> 4   Paul  pear
#> 5   Mary apple
#> 6   Mary  pear

# With expand.grid() character strings are converted to factors by default...
str(grid_1)
#> 'data.frame':	6 obs. of  2 variables:
#>  $ person: Factor w/ 3 levels "Peter","Paul",..: 1 2 3 1 2 3
#>  $ fruit : Factor w/ 2 levels "apple","pear": 1 1 1 2 2 2
#>  - attr(*, "out.attrs")=List of 2
#>   ..$ dim     : Named int [1:2] 3 2
#>   .. ..- attr(*, "names")= chr [1:2] "person" "fruit"
#>   ..$ dimnames:List of 2
#>   .. ..$ person: chr [1:3] "person=Peter" "person=Paul" "person=Mary"
#>   .. ..$ fruit : chr [1:2] "fruit=apple" "fruit=pear"

# ... with expandGrid() character strings are not converted by default.
# Also, there is no attribute "out.attrs" as it is set by expand.grid().
str(grid_2)
#> 'data.frame':	6 obs. of  2 variables:
#>  $ person: chr  "Peter" "Peter" "Paul" "Paul" ...
#>  $ fruit : chr  "apple" "pear" "apple" "pear" ...