# Function like expand.grid in R

**URL:** https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350
**Category:** General Usage
**Tags:** question
**Created:** [June 19, 2017, 5:37pm UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350 "2017-06-19T17:37:58Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [May 28, 2021, 9:34am UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/21 "2021-05-28T09:34:45Z")

</div>

I think the simplest is to iterate the `Iterators.product`, which gives `Tuple`s (that is, it loses name information). Then you can convert on the fly to named tuples, and pass the resulting iterator to `DataFrame`. For example

```julia
function expand_grid(; kws...)
    names, vals = keys(kws), values(kws)
    return DataFrame(NamedTuple{names}(t) for t in Iterators.product(vals...))
end

```

---

<div class="post-metadata">

### Author: ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)
#### Post date: [July 10, 2023, 1:16pm UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/22 "2023-07-10T13:16:19Z")

</div>

It would be nice to see this feature added in a “base” package like [StatsModels.jl](https://github.com/JuliaStats/StatsModels.jl) !

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 10, 2023, 6:32pm UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/23 "2023-07-10T18:32:45Z")

</div>

There a DataFrames.jl function added in the past year called `allcombinations`. See [here](https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.allcombinations)

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 10, 2023, 10:23pm UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/24 "2023-07-10T22:23:52Z")

</div>

other two ways with and without zip

```julia

julia> a=1:2
1:2

julia> b='a':'c'
'a':1:'c'

julia> c=[1,2,3,4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> r(v,n)= hvncat((ones(Int,n-1)...,size(v)...), true,v...)
r (generic function with 1 method)

julia> DataFrame(NamedTuple.(zip.([[:a,:b,:c]],tuple.(r(a,1),r(b,2),r(c,3))[:])))
24×3 DataFrame
 Row │ a b c     
     │ Int64 Char Int64
─────┼────────────────────
   1 │ 1 a 1
   2 │ 2 a 1
   3 │ 1 b 1
   4 │ 2 b 1
  ⋮ │ ⋮ ⋮ ⋮
  22 │ 2 b 4
  23 │ 1 c 4
  24 │ 2 c 4
           17 rows omitted

julia> DataFrame(tuple.(r(a,1),r(b,2),r(c,3))[:])
24×3 DataFrame
 Row │ 1 2 3     
     │ Int64 Char Int64
─────┼────────────────────
   1 │ 1 a 1
   2 │ 2 a 1
   3 │ 1 b 1
   4 │ 2 b 1
  ⋮ │ ⋮ ⋮ ⋮
  22 │ 2 b 4
  23 │ 1 c 4
  24 │ 2 c 4
           17 rows omitted

julia> rename(DataFrame(tuple.(r(a,1),r(b,2),r(c,3))[:]),[:a,:b,:c])
24×3 DataFrame
 Row │ a b c     
     │ Int64 Char Int64
─────┼────────────────────
   1 │ 1 a 1
   2 │ 2 a 1
   3 │ 1 b 1
   4 │ 2 b 1
  ⋮ │ ⋮ ⋮ ⋮
  22 │ 2 b 4
  23 │ 1 c 4
  24 │ 2 c 4
           17 rows omitted

```

or better this way

```julia
r(v,n)= hvncat(n,v...)
DataFrame(NamedTuple{(:a,:b,:c)}.(tuple.(r.([a,b,c],1:3)...)[:]))

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [July 10, 2023, 11:07pm UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/25 "2023-07-10T23:07:49Z")

</div>

Another method is using `crossjoin` from DataFrames:

```julia
julia> (Dict(x) for x in pairs((;a = 1:2, b = 'a':'c', c=[1,2,3,4]))) .|>
         splat(DataFrame) |> splat(crossjoin)
24×3 DataFrame
 Row │ a b c     
     │ Int64 Char Int64 
─────┼────────────────────
   1 │ 1 a 1
   2 │ 1 a 2
   3 │ 1 a 3
   4 │ 1 a 4
   ⋮ │ ⋮ ⋮ ⋮

```

Note the easy no-nonsense syntax specifying `a`,`b`,`c`.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 11, 2023, 7:25am UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/26 "2023-07-11T07:25:41Z")

</div>

I reproduce this to show that I understand how your elegant solution works.

```julia
crossjoin(DataFrame.((pairs((;a,b,c))...,))...)

```

Instead I did not understand the meaning of the note (please note that my English is very poor: I use google translator to write)

> [@Dan](#):
>
> Note the easy no-nonsense syntax specifying `a`,`b`,`c`.

---

<div class="post-metadata">

### Author: ![gabehassler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabehassler/32/13848_2.png) [@gabehassler](https://discourse.julialang.org/u/gabehassler)
#### Post date: [September 22, 2025, 11:20pm UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/27 "2025-09-22T23:20:42Z")

</div>

The `allcombinations` function in the DataFrames package now does this: [Functions · DataFrames.jl](https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.allcombinations)

```julia-auto
julia> using DataFrames

julia> allcombinations(DataFrame, a = 2:4, b = ['a', 'b'])
6×2 DataFrame
 Row │ a b    
     │ Int64 Char 
─────┼─────────────
   1 │ 2 a
   2 │ 3 a
   3 │ 4 a
   4 │ 2 b
   5 │ 3 b
   6 │ 4 b

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 23, 2025, 7:56am UTC](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/28 "2025-09-23T07:56:57Z")

</div>

> [@gabehassler](#):
>
> The `allcombinations` function in the DataFrames package now does this

Apparently, it has been doing that for two years.  
See the [post above](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350/23).

[Previous page](https://discourse.julialang.org/t/function-like-expand-grid-in-r/4350.md?page=1)
