c = combine(groupby(df, :A),:B => mean => :B_mean)
but in such a way that the output does not only show :A and :B_mean but also the other variables that df had (well, at least the variables that can also be combined due to repetition)?
DataFrames canβt know your intent beyond what you program. It canβt intuit which variables are invariant withing an βAβ grouping unless you program that.
You could write:
vars_sub = Symbol.(setdiff(names(df), ["B", "A"]) )
c = combine(groupby(df, :A), :B => mean => :B_mean, (vars_sub .=> first )...)
Thanks for the great answers. I wonder whether there could be a shortcut for this. Itβs probably not so rare that you have not only multiple factors and levels, but also multiple observations per subject and you want to group by ID and e.g. average some result.
In plain English, it reads: For each column in the grouped dataframe (gdf), excluding the grouping variable (:A) and the aggregated column (:B), take the first value within each group.