Yes, unlike dplyr, DataFramesMeta.jl does not allow using previously-created variables in the same call.
You would think the solution is to use the @astable macro-flag, which lets you create multiple intermediate variables and return a NamedTuple. But this is not the solution
julia> @by culture [:year, :ind] begin
@astable begin
:total = sum(:value)
:w = :value ./ :total
end
end
ERROR: ArgumentError: mixing single values and vectors in a named tuple is not allowed
This is because :total is a scalar while :w is a Vector. When returning a NamedTuple you can’t mix scalars and vectors. See some discussion here.
I think what you want is a @chain with multiple @transform calls, the first one takes in a GroupedDataFrame but preserves the number of rows in the data frame (unlike dplyr, the output drops the grouping by default, which I think is very convenient).
julia> @chain culture begin
@groupby [:year, :ind]
@transform :total = sum(:value) # This gets "spread"
@rtransform :w = :value / :total
end
6×5 DataFrame
Row │ ind year value total w
│ Int64 Int64 Int64 Int64 Float64
─────┼──────────────────────────────────────
1 │ 1 2015 10 60 0.166667
2 │ 1 2015 20 60 0.333333
3 │ 1 2015 30 60 0.5
4 │ 2 2016 26 127 0.204724
5 │ 2 2016 34 127 0.267717
6 │ 2 2016 67 127 0.527559