【发布时间】:2020-07-08 17:40:55
【问题描述】:
我想将字符串用作变量名,例如:
var <- 'vs'
var_max <-max(mtcars[, 8], na.rm = T)
#change the max value of the 'vs' column
my_mtcars <- mtcars %>%
mutate(get(var) = ifelse(get(var) == var_max, 100, get(var)))
但这会返回:
Error: unexpected '=' in:
"my_mtcars <- mtcars %>%
mutate(get(var) ="
另一种不使用字符串的方法是:
var_max = max(mtcars$vs)
my_mtcars = mtcars %>% mutate(vs = ifelse(vs == var_max, 100, vs))
【问题讨论】:
标签: r dplyr tidyverse tidyeval