0

I'm experimenting dynamic variable name. Comparing

iris %>% mutate(Species.x=Species)

and

iris %>% mutate(Species.x=(!!"Species"))

They don't match because the latter uses Species as the entire column. Why? (I'm just trying to understand the reasoning and what I really want is to mutate variables dynamically. I saw this post Use dynamic name for new column/variable in `dplyr` but it didn't work. Is it because I'm using a newer version of dplyr?)

monotonic
  • 394
  • 4
  • 20
  • 2
    Use `.data` `iris %>% mutate(Species.x=(.data[["Species"]]))` or convert to `sym`bol and evaluate (`!!`) `iris %>% mutate(Species.x=!!rlang::sym("Species"))` – akrun Jun 02 '22 at 17:51
  • What's the purpose of `sym` and why isn't `!!` working immediately? – monotonic Jun 02 '22 at 17:52
  • 1
    I've never seen `!!` used directly on a string. Have you read through the `?"!!"` help page? – Gregor Thomas Jun 02 '22 at 17:57
  • 1
    You may check the [vignette](https://rlang.r-lib.org/reference/topic-metaprogramming.html) regarding this – akrun Jun 02 '22 at 18:01

0 Answers0