tidy eval with e.g. mtcars %>% mutate(target := log(target))

房东的猫 提交于 2021-01-28 22:06:17

问题


I figured this out while typing my question, but would like to see if there's a cleaner, less code way of doing what I want.

e.g. code block:

target <- "mpg"

# want
mtcars %>% 
  mutate(target := log(target))

I'd like to update mpg to be the log of mpg based on the variable target.

Looks like I got this working with:

mtcars %>% 
  mutate(!! rlang::sym(target) := log(!! rlang::sym(target)))

That just reads as pretty repetitive. Is there a 'cleaner', less code way of achieving the same result?

I'm fond of the double curly braces {{var}}, no reason, they are just nicer to read imho but I couldn't get the same results when I tried:

mtcars %>% 
  mutate(!! rlang::sym(target) := log({{target}}))

What are the various ways I can use tidyeval to mutate a field via transformation based on a pre determined variable to define which field to be transformed, in this case the variable 'target'?


回答1:


On the lhs of :=, the string can be evaluated with just !!, while on the rhs, it is the value that we need, so we convert to symbol and evaluate (!!)

library(dplyr)
mtcars %>% 
     mutate(!!target := log(!! rlang::sym(target)))



回答2:


1) Use mutate_at

library(dplyr)
mtcars %>% mutate_at(target, log)

2) We can use the magrittr %<>% operator:

library(magrittr)
mtcars[[target]] %<>% log

3) Of course this is trivial in base R:

mtcars[[target]] <- log(mtcars[[target]])


来源:https://stackoverflow.com/questions/60048434/tidy-eval-with-e-g-mtcars-mutatetarget-logtarget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!