0

Given this dataframe:

t1 <- data.frame(x=c(1,2,3), y = c(2,3,4))

I would like to get the output as in this function:

t1 %>% mutate_('y' = 'y * 2')

But in such a way that I can define the input somewhere else:

t2 <- 'y'
t3 <- 'y * 2'
t1 %>% mutate_(t2 = t3) # Should produce output as t1 %>% mutate_('y' = 'y * 2')

Thanks for your help!

Best, Jan

Jan Janiszewski
  • 432
  • 3
  • 14
  • There are lots of questions on SO addressing this topic: [dplyr string as column reference](https://stackoverflow.com/questions/27994985/dplyr-string-as-column-reference) [Programming with dplyr using string](https://stackoverflow.com/questions/44121728/programming-with-dplyr-using-string-as-input) [Pass a string as variable name in dplyr::filter](https://stackoverflow.com/questions/48219732/pass-a-string-as-variable-name-in-dplyrfilter) [Using a string as an argument to mutate verb](https://stackoverflow.com/questions/49469982/r-using-a-string-as-an-argument-to-mutate-verb-in-dplyr) – caldwellst Mar 31 '20 at 17:01
  • 2
    See https://dplyr.tidyverse.org/articles/programming.html – r2evans Mar 31 '20 at 17:02

1 Answers1

1

This code reproduces the dataframe from the above question:

t1 %>% dplyr::mutate_(.dots = setNames(t3, t2))

Jan Janiszewski
  • 432
  • 3
  • 14