I have a data frame called "Something". I am doing an aggregation on one of the numeric columns using summarise, and I want the name of that column to contain "Something" - data frame title in the column name.
Example:
temp <- Something %>%
group_by(Month) %>%
summarise(avg_score=mean(score))
But i would like to name the aggregate column as "avg_Something_score". Did that make sense?
We can use the devel version of
dplyr
(soon to be released0.6.0
) that does this withquosure
sHere, the
enquo
takes the input arguments likesubstitute
frombase R
and converts toquosure
, withquo_name
, we can convert it to string, evaluate thequosure
by unquoting (!!
orUQ
) insidegroup_by/summarise/mutate
etc. The column names on the lhs of assignment (:=
) can also evaluated by unquoting to get the columns of interestIt seems like it makes more sense to generate the new column name dynamically so that you don't have to hard-code the name of the data frame inside
setNames
. Maybe something like the function below, which takes a data frame, a grouping variable, and a numeric variable:Now let's run the function on two different data frames:
You can use
rename_
fromdplyr
withdeparse(substitute(Something))
like this:You could use colnames(Something)<-c("score","something_avg_score")