DataFrame to multidimensional array

I assume you know the sizes of x, y and z? Inferring those from the vectors first would be a more annoying step.

df = DataFrame([(a = x, b = y, c = z,d = x+y+z) for x in 1:6 for y in 1:4 for z in 1:2])

arr = permutedims(reshape(copy(df.d), (2, 4, 6)), (3, 2, 1))

This gives:

6×4×2 Array{Int64, 3}:
[:, :, 1] =
 3  4   5   6
 4  5   6   7
 5  6   7   8
 6  7   8   9
 7  8   9  10
 8  9  10  11

[:, :, 2] =
 4   5   6   7
 5   6   7   8
 6   7   8   9
 7   8   9  10
 8   9  10  11
 9  10  11  12

julia> arr == [x+y+z for x in 1:6, y in 1:4 , z in 1:2]
true

Note that for x in 1:6 for y in 1:4 for z in 1:2 has exactly the opposite order of dimensions than for x in 1:6, y in 1:4, z in 1:2 which is why the permutedims is needed.

2 Likes