1

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.

russellpierce
  • 4,583
  • 2
  • 32
  • 44
  • I'm going to leave it open without modification for now. The other question is phrased around rename where this is phrased around mutate. Although the answer is the same, it didn't come up when I was searching or when I was typing this question. – russellpierce Aug 22 '17 at 02:18

1 Answers1

3

Use := with !!, not just =

cars %>%
  mutate(!!conversion_scale_name := speed * conversion_scale_ratio)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Several things to correct. I had a couple mistakes in my original post, please change `conversion_scale_name` to `conversion_scale_sym` and `mph` to `speed`. Next, it looks like your answer truncated the `ca` from `cars` would you please add it? Lastly, it looks like `:=` unpacks the sym so the () and !! aren't needed and actually cause an error. If you'd be so kind as to tweak your answer to address these issues I can give you the check mark. `:=` was the secret sauce I was missing. – russellpierce Aug 21 '17 at 22:05
  • You shouldn't need `sym` for this use. A character value is fine. – MrFlick Aug 21 '17 at 22:08
  • Right you are! Thanks & Accepted. – russellpierce Aug 22 '17 at 02:16