简体   繁体   English

R dplyr:当...将引用数据中的变量名称时,如何使用... with summarise(across())?

[英]R dplyr: how to use ... with summarize(across()) when ... will refer to a variable name within the data?

I want to have a flexible function using summarize in which:我想要使​​用summarize的灵活功能,其中:

  1. the aggregation function is given by user聚合函数由用户给出
  2. the aggregation function might use further arguments which refer to variables within the data itself.聚合函数可能会使用更多参数来引用数据本身中的变量。

A good example is the user providing fun=weighted.mean() and specifying the weight argument w .一个很好的例子是用户提供fun=weighted.mean()并指定权重参数w

For now, I am trying with the ... .现在,我正在尝试使用... . The problem is that I don't find a way to have that ... refer to a variable within the data-frame?问题是我没有找到一种方法来...引用数据框中的变量? The example below is given using across() , but the same happens if I use instead summarize_at()下面的示例是使用across()给出的,但如果我改为使用summarize_at()也会发生同样的情况

Thanks!!谢谢!!

library(tidyverse)
fo1 <- function(df, fun=mean, ...){
  df %>% 
    group_by(Species) %>% 
    summarise(across(starts_with("sepal"), fun, ...))
}

fo1(iris)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 3
#>   Species    Sepal.Length Sepal.Width
#>   <fct>             <dbl>       <dbl>
#> 1 setosa             5.01        3.43
#> 2 versicolor         5.94        2.77
#> 3 virginica          6.59        2.97
fo1(iris, fun=weighted.mean)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 3
#>   Species    Sepal.Length Sepal.Width
#>   <fct>             <dbl>       <dbl>
#> 1 setosa             5.01        3.43
#> 2 versicolor         5.94        2.77
#> 3 virginica          6.59        2.97
fo1(iris, fun=weighted.mean, w=Petal.Length)
#> Error: Problem with `summarise()` input `..1`.
#> x object 'Petal.Length' not found
#> ℹ Input `..1` is `across(starts_with("sepal"), fun, ...)`.
#> ℹ The error occurred in group 1: Species = "setosa".
fo1(iris, fun=weighted.mean, w=.data$Petal.Length)
#> Error: Problem with `summarise()` input `..1`.
#> x 'x' and 'w' must have the same length
#> ℹ Input `..1` is `across(starts_with("sepal"), fun, ...)`.
#> ℹ The error occurred in group 1: Species = "setosa".

Created on 2020-11-10 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 11 月 10 日创建

You need to pass the exact value of additional arguments.您需要传递附加参数的确切值。 .data$Petal.Length is NULL . .data$Petal.LengthNULL

library(dplyr)

fo1 <- function(df, fun=mean, ...){
  df %>% 
    summarise(across(starts_with("sepal"), fun, ...))
}


fo1(iris, fun=weighted.mean, w= iris$Petal.Length)
#  Sepal.Length Sepal.Width
#1     6.180167    2.970197

This is ugly, but works.这很丑陋,但有效。

> fo1 <- function(df, fun=mean, ...){
+   w <- df %>% pull(...)
+   df %>% 
+     summarise(across(starts_with("Sepal"), fun, w))
+ }
> fo1(iris, fun=weighted.mean, Petal.Length)
  Sepal.Length Sepal.Width
1     6.180167    2.970197

Following on from Paul's suggestion in the comments above, this seems to be a general solution:继保罗在上述评论中的建议之后,这似乎是一个通用的解决方案:

fo1 <- function(df, fun=mean, ...){
  df %>% 
    summarise(across(starts_with("Sepal"), fun, !!!enquos(...)))
}
> fo1(iris, fun=weighted.mean, Petal.Length)
  Sepal.Length Sepal.Width
1     6.180167    2.970197
> fo1(iris, fun=mean)
  Sepal.Length Sepal.Width
1     5.843333    3.057333

I tried several combinations of !!我尝试了几种组合!! , !!! !!! , enquo() and enquos() but must have missed that one. , enquo()enquos()但一定错过了那个。

enquos will return a list of quoted expressions. enquos将返回带引号的表达式列表。 The unquote-splice operator, !!!取消引用拼接运算符, !!! , will unquote each element as an argument to the function call. , 将取消引用每个元素作为函数调用的参数。

library(tidyverse)

fo1 <- function(df, fun = mean, ...) {
  df %>% 
    summarise(across(starts_with("sepal"), fun, !!!enquos(...)))
}

iris %>%
  group_by(Species) %>%
  fo1(fun = weighted.mean, w = Petal.Length, na.rm = TRUE)
#> # A tibble: 3 x 3
#>   Species    Sepal.Length Sepal.Width
#>   <fct>             <dbl>       <dbl>
#> 1 setosa             5.02        3.44
#> 2 versicolor         5.98        2.79
#> 3 virginica          6.64        2.99

See here for more info.请参阅此处了解更多信息。

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

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