With DataFrames, best practice for applying function across columns, where we also need to reference, in a second argument, the same column for each function call?

Here is a minimal, complete example for what I think you are trying to accomplish:

using DataFrames, Statistics
df = DataFrame(x = repeat(1 : 3; outer = 4), y = repeat(22.0 :23.0; outer = 6), z = 1.0:12.0, wt = 2.0:13.0);
gdf = groupby(df, :x);
nms = [:y, :z];
nmPairs = [[nm, :wt] for nm in nms];
wt_mean(x,y) = mean(x, FrequencyWeights(y));
combine(gdf, nmPairs .=> wt_mean .=> nms)
3×3 DataFrame
 Row │ x      y        z
     │ Int64  Float64  Float64
─────┼─────────────────────────
   1 │     1  22.6154  7.23077
   2 │     2  22.4     8.0
   3 │     3  22.5882  8.82353

I replaced mean(x; weights) b/c that method seems undefined (at least in Statistics.jl).

3 Likes