Since this was revived, hereβs one way:
julia> using DataFrames, AxisKeys
julia> df = DataFrame(rand(1:10, 3, 4), [:i, :j, :k, :z])
3Γ4 DataFrame
Row β i j k z
β Int64 Int64 Int64 Int64
ββββββΌββββββββββββββββββββββββββββ
1 β 10 3 9 4
2 β 5 7 4 2
3 β 10 2 4 3
julia> wrapdims(df, :z, :i, :j, :k; default=missing, sort=true)
3-dimensional KeyedArray(NamedDimsArray(...)) with keys:
β i β 2-element Vector{Int64}
β j β 3-element Vector{Int64}
β‘ k β 2-element Vector{Int64}
And data, 2Γ3Γ2 Array{Union{Missing, Int64}, 3}:
[:, :, 1] ~ (:, :, 4):
(2) (3) (7)
(5) missing missing 2
(10) 3 missing missing
[:, :, 2] ~ (:, :, 9):
(2) (3) (7)
(5) missing missing missing
(10) missing 4 missing
julia> df2 = DataFrame(rand(Int8, 3, 4), [:i, :j, :k, :z])
3Γ4 DataFrame
Row β i j k z
β Int8 Int8 Int8 Int8
ββββββΌββββββββββββββββββββββββ
1 β 119 93 -48 -70
2 β -78 -115 -73 -61
3 β 42 -122 97 -92
julia> wrapdims(df2, :z, :i, :j, :k; default=0)
3-dimensional KeyedArray(NamedDimsArray(...)) with keys:
β i β 3-element Vector{Int8}
β j β 3-element Vector{Int8}
β‘ k β 3-element Vector{Int8}
And data, 3Γ3Γ3 Array{Union{Int64, Int8}, 3}:
[:, :, 1] ~ (:, :, -48):
(93) (-115) (-122)
(119) -70 0 0
(-78) 0 0 0
(42) 0 0 0
[:, :, 2] ~ (:, :, -73):
(93) (-115) (-122)
(119) 0 0 0
(-78) 0 -61 0
(42) 0 0 0
[:, :, 3] ~ (:, :, 97):
(93) (-115) (-122)
(119) 0 0 0
(-78) 0 0 0
(42) 0 0 -92
This goes via Tables.jl. Could use more polish, ideally would be able to infer :i, :j, :k
, and accept "i", "j", "k"
. but should work. There may be similar functions in the other similar packages.