简体   繁体   English

dplyr替换多个变量中的值

[英]dplyr Replace values in multiple variables

I need to replace non-4 with 80 in cyl, gear, carb columns. 我需要用80英寸的气缸,齿轮和碳水化合物色谱柱替换非4色谱柱。 I tried the following, but it does not work. 我尝试了以下操作,但是不起作用。

mtcars %>% mutate_at(vars(cyl, gear, carb), replace(which(.!=4), 80))

It throws the following error: 它引发以下错误:

Error in replace(which(. != 4), 80) : 
  argument "values" is missing, with no default

What am I missing here? 我在这里想念什么?

You need to pass a function or formula to mutate_at as second argument: 您需要将一个函数或公式传递给mutate_at作为第二个参数:

mtcars %>% mutate_at(vars(cyl, gear, carb), ~ replace(., which(.!=4), 80))

Or create the function using funs : 或使用funs创建函数:

mtcars %>% mutate_at(vars(cyl, gear, carb), funs(replace(., which(.!=4), 80)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM