0

I am trying to dynamically manipulate columns of a data frame like so :

dw <- dw %>% mutate(a := "a + b_{i}")

with a, b_1, b_2, b_3 etc. being column names and i being an index in a loop. This doesn't work and I also failed to make it work using glue(). Is there a way to use glue evaluation and then do some operations with mutate ?

shadok
  • 1
  • 1
  • 2
    Please give a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And show some example data. – Martin Gal Jul 03 '21 at 15:44
  • `mutate` doesn't use `:=` but `=`. – Martin Gal Jul 03 '21 at 15:44
  • 2
    Any reason you want to use `glue` when you could do `mutate(a = a + .data[[paste0("b_", i)]])` using the `.data` pronoun from `rlang` – stefan Jul 03 '21 at 15:48
  • glue produces strings. To use mutate, you need to create expressions. Strings and expressions are quite different so glue is not the right tool for the job. – MrFlick Jul 03 '21 at 19:09

0 Answers0