How to assign a column name as a variable

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

  1. What is best?
  2. 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)

Try using the ensym function rlang package as shown below:

analysis_by_species <- TRUE

if (analysis_by_species) {
  colname <- 'species'
} else {
  colname <- 'homeworld'
}

colname <- rlang::ensym(colname) # this converts your colname string into colname bare name.

average_height <- starwars %>% 
  group_by({{colname}}) %>%  # not the double curly brackets notation- it's called the 'curly-curly' operator.
  summarise(height = mean(height))
print(average_height)

That is exactly what I was looking for. thank you.
{{colname}} is a bit easier to remember than !!sym(colname)

What I would love is to be able to create an object which I pass as-is to group_by. But ensym+curly-curly is good.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.