Search code examples
rdplyrnse

How can you give a character name to a mutate result in dplyr 0.7.x?


Following from a previous question involving filter, I thought the pattern would be...

library(dplyr)
library(rlang)
conversion_scale_name <- "kph"
conversion_scale_ratio <- 1.60934
conversion_scale_sym <- sym(conversion_scale_name)
cars %>%
    mutate((!!conversion_scale_sym) = speed * conversion_scale_ratio)

However that doesn't seem to work. I get

Error: unexpected '=' in:
"    cars %>%
        mutate((!!conversion_scale_sym) ="

What am I doing wrong?

P.S. It turns out a previous question asked a similar question about rename. The solution turns out to be the same.


Solution

  • Use := with !!, not just =

    cars %>%
      mutate(!!conversion_scale_name := speed * conversion_scale_ratio)