R 函数使用 .还有~ [英] R function using . and ~

查看:43
本文介绍了R 函数使用 .还有~的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习在 R 中使用 ~..

I'm trying to learn to use ~ and . in R.

在下面的代码中是使用和不使用~编写的相同函数..我不明白在第一个出现的函数中发生了什么错误.

In the code below is the same function written with and without the use of ~ and ..I didn't understand what happened in the first function to appear the error.

#FIRST FUNCTION
col_summary2 <- function(.x, .f, ...){
  .x <- purrr::keep(.x, is.numeric)
  purrr::map_dbl(.x, ~.f(., ...))
}

col_summary2(mtcars,mean) #Error in mean.default(., ...) : 'trim' must be numeric of length one

#SECOND FUNCTION
col_summary2 <- function(.x, .f, ...){
  .x <- purrr::keep(.x, is.numeric)
  purrr::map_dbl(.x, function(x) .f(x, ...))
}

col_summary2(mtcars,mean) #mpg        cyl       disp         hp       drat         wt       qsec         vs      am       gear       carb
                          #20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750   0.437500 0.406250   3.687500   2.812500 

推荐答案

在第一种情况下,您的参数在内部传递给 purrr::as_mapper:

In the first case your argument is passed to purrr::as_mapper internally:

as_mapper(~f(., ...))
# function (..., .x = ..1, .y = ..2, . = ..1) 
#   f(., ...)

这意味着它的行为就像你写的一样:

Which means it behaves just as if you had written :

purrr::map_dbl(.x, function(..., .x = ..1, .y = ..2, . = ..1) f(., ...))

创建的函数有一个 ... 参数,(它总是有,即使 .f 没有它也会有).

The function that is created has a ... argument, (it always has, it would even if .f didn't have it).

.x.y. 参数实际上是从 ... 中提取的,因为 ..1 表示第一件事塞进 ...",..2 表示第二件事......"等等.

The .x, .y, and . arguments are actually extracted from the ..., as ..1 means "first thing stuffed into ...", ..2 means "second thing..." and so on.

因此,在您的第一种情况下,.x 通过 ... 传递并以 . 结束.

So in your first case .x is passed through ... and ends up in . as well.

col_summary3 <- function(.x, .f, ...){
  .x <- purrr::keep(.x, is.numeric)
  purrr::map_dbl(.x, ~{print(list(...));.f(., ...)})
}
col_summary3(mtcars,mean) 
# [[1]]
# [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4
# [17] 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
#  Error in mean.default(., ...) : 'trim' must be numeric of length one 

.x 最终被提供给 .f 的第一个参数,并通过 .f(.,...),所以它在mean.default的第二个参数中结束,即trim,它不喜欢它.

.x ends up being fed both to the first argument of .f and to the second through .f(.,...), so it ends up in the second argument of mean.default, i.e. trim, which doesn't like it.

在您的第二个函数中情况并非如此,其中 ... 参数(在本例中未使用)从初始调用顺利传递到 .f打电话.

It's not the case in your second function, where the ... argument (not used in this case) is smoothly passed from the initial call to the .f call.

Re那么,在这种情况下,我不能使用 ~ 和 .,对吗?"

您不能直接使用它,因为它与 ... 有冲突.但是,您可以通过将它传递给 .y 参数并使用 purr::invoke 来绕过它,但这对于下一个必须阅读它的人来说非常刻薄:).

You can't use it directly as there is a conflict with .... You can go around it however by passing it to the .y argument and use purr::invoke, but that's very mean for the next person that will have to read it :).

col_summary4 <- function(.x, .f, ...){
  .x <- purrr::keep(.x, is.numeric)
  purrr::map_dbl(.x, ~ invoke(.f, .y, .), .y=list(...))
}

col_summary4(mtcars,mean) 
# mpg        cyl       disp         hp       drat         wt       qsec         vs         am       gear       carb 
# 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750   0.437500   0.406250   3.687500   2.812500 

col_summary4(mtcars,mean, trim = 0.3)
# mpg         cyl        disp          hp        drat          wt        qsec          vs          am        gear        carb 
# 19.1785714   6.4285714 214.5071429 136.2857143   3.5971429   3.2467857  17.7600000   0.3571429   0.2857143   3.5714286   2.6428571 

这篇关于R 函数使用 .还有~的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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