Mathew Lionnet
Mathew Lionnet

Reputation: 11

Use a list in a mutate function in R

I am trying to use a list in a mutate, please see below:

Grouping <- c('f_risk_code', 'f_risk_category')
Pasting <- c('f_risk_code, f_risk_category')

Then using it in here:

Nested_Train %>%
  mutate(Category = paste0(glue_col(Pasting), sep='_'))

But this is not having the desired effect - it is just returning f_risk_code', 'f_risk_category as the Category instead of the actual risk code and risk category fields. Any help appreciated.

Upvotes: 0

Views: 311

Answers (3)

Mathew Lionnet
Mathew Lionnet

Reputation: 11

This was the solution for me: Try something like:

cols <- rlang::exprs(hp, cyl); mtcars %>% mutate(out=paste(!!!cols, sep="_"))

Upvotes: 0

akrun
akrun

Reputation: 887048

With tidyverse, we can use invoke

library(dplyr)
library(purrr)
Nested_Train %>%
     mutate(Category = invoke(paste0, .[Pasting]))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388897

You can use do.call :

library(dplyr)

Nested_Train %>% mutate(Category = do.call(paste0, .[Pasting]))

Upvotes: 1

Related Questions