Hi all,
I have an array of objects that I would like to convert to a DataFrame. Each object has some arrays and some individual values. I would like to destructure the arrays into individual rows and columns and repeat the individual values while maintaining their types. Here is a sample example:
using DataFrames
mutable struct M
id::Int
vals::Array{<:Number,2}
end
ms = [M(i, rand(2,2)) for i in 1:2]
df = DataFrame(ms)
output
Row │ id vals
│ Int64 Array…
─────┼──────────────────────────────────────────
1 │ 1 [0.618182 0.00825483; 0.646895 0…
2 │ 2 [0.450556 0.462105; 0.889429 0.8…
After exploring various approaches this is the closest solution:
rows = mapreduce(x -> [fill(x.id, size(x.vals,1)) x.vals], vcat, ms)
new_df = DataFrame(rows, :auto)
output
4×3 DataFrame
Row │ x1 x2 x3
│ Float64 Float64 Float64
─────┼───────────────────────────────
1 │ 1.0 0.618182 0.00825483
2 │ 1.0 0.646895 0.0341765
3 │ 2.0 0.450556 0.462105
4 │ 2.0 0.889429 0.843017
The main problem is that x1 is not a row of Int. Is there a better/less hacky way to achieve this goal?
I should also note that I do not know the width of the array vals. So I cannot hard code column types. However, they will be the same width for each object.