I wanted to use some dynamic variable names in dplyr and had to look up dplyr programming again.
This is something I have to look up regularly and where I am still not comfortable with the terminology. Here, I find the respective tidyverse documentation rather challenging. However, dplyr programming is also something that I use rarely.
So here is a brief summary of my recent exploration of the topic.
count_var2 <-function(dt, var) {summarise(dt, "{var}_count":=n(),.by =all_of({{ var }}) )}count_var2(cars, "cyl")
cyl
cyl_count
6
7
4
11
8
14
count_var3 <-function(dt, var) {summarise(dt, "{{var}}_count":=n(),.by =all_of({{ var }}) )}count_var3(cars, var_select)
cyl
var_select_count
6
7
4
11
8
14
summarise_dt <-function(data, var) { data |>summarise("mean_{{var}}":=mean({{ var }}),"sum_{{var}}":=sum({{ var }}),"n_{{var}}":=n() )}summarise_dt(cars, cyl)