# Generate a dataset

**URL:** <https://discourse.julialang.org/t/generate-a-dataset/35812>\
**Category:** New to Julia\
**Tags:** tuple, arrays\
**Created:** [March 10, 2020, 9:00pm UTC](https://discourse.julialang.org/t/generate-a-dataset/35812 "2020-03-10T21:00:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![STHossain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sthossain/32/8821_2.png) [@STHossain](https://discourse.julialang.org/u/STHossain)\
**Post date:** [March 10, 2020, 9:00pm UTC](https://discourse.julialang.org/t/generate-a-dataset/35812/1 "2020-03-10T21:00:02Z")

</div>

I am sorry maybe my title of the problem is probably not so appropriate, but here is the problem. So I have some arrays of categorical or boolean values and I want to create a combination of all of them and form an array. Also It could be the case that for each one of these combinations I have a unique value (in the example it is on the `z` vector). I tried with the following example and it worked. But honestly I didn’t like the way I could do this, is there a simpler way to do this?

Suppose I have

```julia
x = [false, true];
y = ['A', 'B'];
z = rand(4);

```

And I want to prodce a two dimensional array like following

```julia
4×3 Array{Any,2}:
 false 'A' 0.961038
  true 'A' 0.518147
 false 'B' 0.210022
  true 'B' 0.543537

```

Here is what I tried

```julia
A = [(i, j) for i in x, j in y];
B = [collect(zip(A[i]..., z[i])) for i in 1:length(A)];

C = reshape([i for i in (B[1]...)], 1, 3);
for j in 2:length(B)
    C = vcat(C, reshape([i for i in (B[j]...)], 1, 3))
end
A

```

---

<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:** [March 10, 2020, 9:05pm UTC](https://discourse.julialang.org/t/generate-a-dataset/35812/2 "2020-03-10T21:05:16Z")

</div>

You are looking for `Iterators.product`.

```julia
x = [false, true];
y = ['A', 'B'];
z = rand(4);

df = DataFrame(a = Bool[], b = Char[], c = Float64[])
for p in Iterators.product(x, y, z)
    push!(df, p)
end

Matrix(df)

```

---

<div class="post-metadata">

**Author:** ![STHossain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sthossain/32/8821_2.png) [@STHossain](https://discourse.julialang.org/u/STHossain)\
**Post date:** [March 10, 2020, 9:06pm UTC](https://discourse.julialang.org/t/generate-a-dataset/35812/3 "2020-03-10T21:06:58Z")

</div>

Hi @pdeffebach, thanks a lot for the reply. So I need to create a DataFrame, could it be independent of Dataframe? Thanks for the solution. This looks way way simpler than mine. But just tried, the code didn’t work, maybe I don’t know the details of `Iterators.product`. I can check

---

<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:** [March 10, 2020, 9:24pm UTC](https://discourse.julialang.org/t/generate-a-dataset/35812/4 "2020-03-10T21:24:52Z")

</div>

Sorry, edited the above so it works. It looks like you have to declare the columns first if you are pushing just `Tuple`s instead of `NamedTuples`.

You don’t have to use `DataFrames`, but its a convenient thing to do, plus you said in your post you are interested in “creating a dataset”.

You can do the same process by initializing an arrray of `Any` with 3 columns an 0 rows.

```julia
julia> df = Array{Any}(undef, 0, 3)
julia> for p in Iterators.product(x, y, z)
           global df # because i'm working in REPL I need to declare global in the loop    
           df = vcat(df, permutedims(collect(p)))
       end

```

---

<div class="post-metadata">

**Author:** ![STHossain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sthossain/32/8821_2.png) [@STHossain](https://discourse.julialang.org/u/STHossain)\
**Post date:** [March 10, 2020, 9:31pm UTC](https://discourse.julialang.org/t/generate-a-dataset/35812/5 "2020-03-10T21:31:33Z")

</div>

Hi Peter, thanks, this works actually, so this is what I wanted,

```julia
df = Array{Any}(undef, 0, 2)
for p in Iterators.product(x, y)
    df = vcat(df, permutedims(collect(p)))
end
hcat(df, z)

```
