Create a new variable of concatenated values of other columns using dplyr::mutate and a vector of choice columns

I have this dataframe:

vehicles <- tibble(vehicle = c("Car", "Car", "Motorbike"),
                   color = c("Red", "Black", "Blue"),
                   speed = c("Low", "High", "High"))
vehicle color speed
Car Red Low
Car Black High
Motorbike Blue High

I would like to create a new column that pastes the categorical values of columns that I choose based on a vector.

For instance, if I want to create a categorical column based on vehicle and color, I would do:

vehicles %>% mutate(category = paste(vehicle, color, sep = "_"))

Which would give me:

vehicle color speed category
Car Red Low Car_Red
Car Black High Car_Black
Motorbike Blue High Motorbike_Blue

However, I want to embed this code into a function, so that the columns to paste are determined by a character vector provided by the user.

E.g., user chooses choices = c("vehicle", "color", "speed"). But dplyr’s tidy evaluation does not allow me to do vehicles %>% mutate(category = paste(choices, sep = "_")), because that would simply create a column vector full of "vehicle_color_speed" values. How would I then create something with the same result as:

vehicles %>% mutate(category = paste(vehicle, color, speed, sep = "_"))

but using the choices vector.

>Solution :

With rlang::syms:

choices = c("vehicle", "color", "speed")
vehicles %>% 
  mutate(category = paste(!!!rlang::syms(choices), sep = "_"))


# A tibble: 3 × 4
  vehicle   color speed category           
  <chr>     <chr> <chr> <chr>              
1 Car       Red   Low   Car_Red_Low        
2 Car       Black High  Car_Black_High     
3 Motorbike Blue  High  Motorbike_Blue_High

Leave a Reply