Dear Julia community,
I wonder if one can reproduce the nest
and unnest
functionality of TidyR. I have seen the nice related blog Nesting and unnesting columns in DataFrames.jl | Blog by Bogumił Kamiński, which however ‘only’ seems to work over Tuples.
A MWE:
df = DataFrame(rand(100,3), :auto)
df.gr=repeat('A':'D'; inner=25)
## Create a simple nested DataFrame
df_nested = combine(groupby(df, :gr), [:x1, :x2, :x3] => ((x,y,z) -> Ref(DataFrame(x1=x, x2=y, x3=z))) => :X_DataFrame)
julia> df_nested
4×2 DataFrame
Row │ gr X_DataFrame
│ Char DataFrame
─────┼──────────────────────
1 │ A 25×3 DataFrame
2 │ B 25×3 DataFrame
3 │ C 25×3 DataFrame
4 │ D 25×3 DataFrame
Questions:
- How can I revert df_nested into df? I tried:
transfrom(dfc, :X_DataFrame => AsTable)
orcombine(dfc, :X_DataFrame => AsTable)
for instance, but get errorkeys(::DataFrame) does not exist
. - How can I create a more general function, which does the nesting instead of doing it by hand? Something like:
nest(gdf:GroupedDataFrame, cols)
Sorry, I still don’t have the best intuition, of the great DataFrames package…