使用 dplyr::mutate_at 和 case_when 以另一个变量为条件

Using dplyr::mutate_at with case_when conditional on another variable

我想根据条件以不同方式转换一组变量。我找到了解决方案

示例:如果'gear'等于4,我想从所有变量'mpg'减去4到'disp',否则我减去3。

mtcars %>% 
  mutate_at(vars(mpg:disp),
            funs(case_when(gear == 4 ~ . - 4L, TRUE ~ . -3L))) 

但是我收到这个警告:

funs() is soft deprecated as of dplyr 0.8.0
Please use a list of either functions or lambdas: 

# Simple named list: 
`list(mean = mean, median = median)`
# Auto named with `tibble::lst()`: 
`tibble::lst(mean, median)`
# Using lambdas
`list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))`

This warning is displayed once per session.  

是否有更好的解决方案来避免使用 funs()

重复这样做

mtcars %>% mutate_at(vars(mpg:disp), ~ case_when(gear == 4 ~ . - 4L, TRUE ~ . -3L))