0

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Don’t try to highlight code. – IRTFM May 06 '21 at 05:00
  • 1
    So are `f_risk_code` and `f_risk_category` columns in your data.frame? What exactly is the desired output? The `glue_col` function is for constructing strings with color. Are you trying to create colors here somehow? What does the version that doesn't use a vector look like? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 06 '21 at 05:05
  • Yes, they are columns in the Nested_Train - and i want to use them as outputs without stipulating the column names – Mathew Lionnet May 06 '21 at 05:18
  • If i do the following it does what i want: Nested_Train %>% mutate(Category = paste(f_risk_code, f_risk_category, sep = '_')) – Mathew Lionnet May 06 '21 at 05:21
  • Try something like: `cols <- rlang::exprs(hp, cyl); mtcars %>% mutate(out=paste(!!!cols, sep="_"))` – MrFlick May 06 '21 at 05:27
  • That worked, thank you! – Mathew Lionnet May 06 '21 at 05:30

3 Answers3

1

You can use do.call :

library(dplyr)

Nested_Train %>% mutate(Category = do.call(paste0, .[Pasting]))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

With tidyverse, we can use invoke

library(dplyr)
library(purrr)
Nested_Train %>%
     mutate(Category = invoke(paste0, .[Pasting]))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

This was the solution for me: Try something like:

cols <- rlang::exprs(hp, cyl); mtcars %>% mutate(out=paste(!!!cols, sep="_"))
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41