I have a medium-sized block of code where sometimes I want to group_by and summarise by one column, sometimes another. I would like to switch between one and the other without a global edit.
I can take that string and pass !!sym(colname) to group_by
Questions
- What is best?
- Is there a way of assigning !!sym(colname) to an object and just passing that. It is easier to see what is being done.
I think I could achieve the whole thing by putting it all in a function, passing in the column name and using enquo etc
Code to try and show what I mean:
library("dplyr")
# standard hardcoded specification of column name
average_height_by_species <- starwars %>%
group_by(species ) %>%
summarise(height = mean(height))
print(average_height_by_species)
# standard hardcoded specification of column name
average_height_by_species <- starwars %>%
group_by("species") %>%
summarise(height = mean(height))
print(average_height_by_species)
# assign required column name to a variable to enable switching
analysis_by_species <- TRUE
if (analysis_by_species) {
colname <- 'species'
} else {
colname <- 'homeworld'
}
average_height <- starwars %>%
group_by(!!sym(colname) ) %>%
summarise(height = mean(height))
print(average_height)
# what do I enter here to switch
column_specifier <- xxx
average_height = starwars %>%
group_by(column_specifier ) %>%
summarise(height = mean(height))
print(average_height)