简体   繁体   English

R-来自dplyr的mutate_if中的自有函数

[英]R - own function in mutate_if from dplyr

In order to use the function mutate_if() from dplyr , I only find a way by defining the functions upstream, like this: 为了使用mutate_if() 函数 ,我只能通过定义上游函数来找到一种方法,如下所示:

library(dplyr)    
data(iris)

f1 <- function(x){ max(x) < 1 }
f2 <- function(x){ x / max(x) }

df <- iris %>%
   mutate_if(f1, f2)

Is there a way, like with the arguments vars() and funs() in mutate_at() , to define the function within the mutate_if() , and not necessary before ? 有没有一种方法,就像参数vars()funs()mutate_at()在中定义的函数mutate_if()之前,而不是必要的?

Sure, you can use anonymous functions in the mutate_if call: 当然,您可以在mutate_if调用中使用匿名函数:

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

# mimic use of is_numeric, then add +2 to each column
iris %>%
    mutate_if(function(x) is.numeric(x),
              function(x) x + 2) %>%
    head

# Or using the newer syntax (thanks to @akrun):
iris %>%
    mutate_if(~ is.numeric(.),
              ~ . + 2) %>%
    head

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          7.1         5.5          3.4         2.2  setosa
2          6.9         5.0          3.4         2.2  setosa
3          6.7         5.2          3.3         2.2  setosa
4          6.6         5.1          3.5         2.2  setosa
5          7.0         5.6          3.4         2.2  setosa
6          7.4         5.9          3.7         2.4  setosa

In your case, you max gives an error if it's given non-numeric data, so we need to check that it's numeric before going into your real test, but it works just fine this way: 在您的情况下,如果给定了非数字数据,则max会给出错误,因此我们需要在进行实际测试之前检查它是否为数字,但这种方式可以正常工作:

iris %>%
    mutate_if(function(x) if (is.numeric(x)) max(x) > 1 else FALSE,
              function(x) x / max(x)) %>%
    head

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1    0.6455696   0.7954545    0.2028986        0.08  setosa
2    0.6202532   0.6818182    0.2028986        0.08  setosa
3    0.5949367   0.7272727    0.1884058        0.08  setosa
4    0.5822785   0.7045455    0.2173913        0.08  setosa
5    0.6329114   0.8181818    0.2028986        0.08  setosa
6    0.6835443   0.8863636    0.2463768        0.16  setosa

Just want to note that you can also use the purrr notation: 只需要注意,您还可以使用purrr表示法:

iris %>%
  mutate_if(~ if (is.numeric(.x)) max(.x) > 1 else FALSE,
            ~ .x / max(.x)) 

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

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