Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

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

output

# 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading