Mutate_if语法帮助-如何在谓词条件下为函数添加参数 [英] Mutate_if syntax help - how to add in parameter for function on predicate condition

查看:168
本文介绍了Mutate_if语法帮助-如何在谓词条件下为函数添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找有关如何使用dplyr的mutate_if语句以查看是否需要将列转换为因子变量的建议。

Looking for some advice on how to use dplyr's mutate_if statement to check to see if I need to convert a column to a factor variable.

此函数说明了我在努力问题是,当我想为函数中的 max_value参数传递内容时,正确的语法是什么?

This is function illustrates what I'm trying to do. The problem is, what is the correct syntax when I want to pass in something for the "max_value" parameter in my function?

不起作用-我尝试在函数内更改参数。

Doesn't work - I try to change parameter within function.

funct_change <- function(x, max_value ){
  max(x, na.rm = TRUE) >max_value 
  }


mtcars %>% mutate_if( funct_change(max_value=30), as.character) %>% glimpse()

作品-我对参数进行了硬编码

Works - I hardcode the parameter

funct_change <- function(x, max_value=30 ){
      max(x, na.rm = TRUE) >max_value 
      }


    mtcars %>% mutate_if( funct_change, as.character) %>% glimpse()


推荐答案

如果您要提供的不是裸函数名称, .predicate mutate_if 中(以及 .funs 在 dplyr 中种植),您需要执行以下操作之一:

If you're supplying anything other than a bare function name to .predicate in mutate_if (and also other places that .funs crops up in dplyr), you need to do one of the following:


  1. 将函数包装在匿名函数中

  2. 使用速记公式syn征税基本上可以做同样的事情。

  3. 此处不适用,但有时 ... 允许您向 .funs 添加额外的参数,因此您可以提供 max_value = 30 作为参数进行变异。对于 mutate_if ,这仅适用于要应用的函数,而不适用 .predicate

  1. Wrap the function in an anonymous function
  2. Use the shorthand formula syntax ~ to do basically the same thing.
  3. Not applicable here, but sometimes ... lets you add extra arguments to .funs, so you could supply max_value = 30 as an argument to mutate. For mutate_if, that only works for the function to apply, not the .predicate.





funct_change <- function(x, max_value){
  max(x, na.rm = TRUE) > max_value 
}
library(dplyr)
mtcars %>% mutate_if(function(x) funct_change(x, 30), as.character) %>% glimpse()
#> Observations: 32
#> Variables: 11
#> $ mpg  <chr> "21", "21", "22.8", "21.4", "18.7", "18.1", "14.3", "24.4...
#> $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
#> $ disp <chr> "160", "160", "108", "258", "360", "225", "360", "146.7",...
#> $ hp   <chr> "110", "110", "93", "110", "175", "105", "245", "62", "95...
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
#> $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
#> $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
mtcars %>% mutate_if(~ funct_change(., 30), as.character) %>% glimpse()
#> Observations: 32
#> Variables: 11
#> $ mpg  <chr> "21", "21", "22.8", "21.4", "18.7", "18.1", "14.3", "24.4...
#> $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
#> $ disp <chr> "160", "160", "108", "258", "360", "225", "360", "146.7",...
#> $ hp   <chr> "110", "110", "93", "110", "175", "105", "245", "62", "95...
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
#> $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
#> $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...

reprex包(v0.2.0)。

这篇关于Mutate_if语法帮助-如何在谓词条件下为函数添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆