使用 tidyverse 和 sapply 在列表上创建一个函数

Making a function over a list with tidyverse and sapply

我正在尝试构建一个函数来获取列表中不同数据帧中同一变量的加权平均值。该函数不接受一些参数(工资和体重),我相信存在“”或 [[]] 问题,但我似乎无法使其工作。

这是给我错误的可重现示例

set.seed(555)
lista <- list(A = data.frame(wage = (runif(10, min=50, max=100)), weight = (runif(10, min=0, max=1))),
B = data.frame(wage = (runif(10, min=55, max=105)), weight = (runif(10, min=0.1, max=1))))
list


wmeanf <- function(df, x, w) {
  mean <- df %>% summarise (weighted.mean(x,w))
  mean
}


twmean <- sapply(lista, function (X) wmeanf (df = X, x = wage, w = weight))

谢谢!

有几种方法可以实现这一点。希望其中之一能让您朝着正确的方向前进:

library(tidyverse)

set.seed(555)
lista <- list(A = data.frame(wage = (runif(10, min=50, max=100)), weight = (runif(10, min=0, max=1))),
              B = data.frame(wage = (runif(10, min=55, max=105)), weight = (runif(10, min=0.1, max=1))))

map(lista, ~ weighted.mean(x = .$wage, w = .$weight))
#> $A
#> [1] 75.60411
#> 
#> $B
#> [1] 70.22652
lapply(lista, function(x) { weighted.mean(x = x$wage, w = x$weight) })
#> $A
#> [1] 75.60411
#> 
#> $B
#> [1] 70.22652
sapply(lista, function(x) { weighted.mean(x = x$wage, w = x$weight) })
#>        A        B 
#> 75.60411 70.22652

reprex package (v0.3.0)

于 2020-05-05 创建

在@Jason 建议查看 here Dplyr 评估和引用后,我找到了一种方法来实现我最初的预期功能:

set.seed(555)
lista <- list(A = data.frame(wage = (runif(10, min=50, max=100)), weight = (runif(10, min=0, max=1))),
              B = data.frame(wage = (runif(10, min=55, max=105)), weight = (runif(10, min=0.1, max=1))))

wmeanf <- function(df, x, w) {

  x <- enquo(x)
  w <- enquo(w)

  mean <- df %>% summarise (weighted.mean(!!x,!!w))
  mean
}

sapply(lista, function (X) wmeanf (df = X, x = wage, w = weight))

$`A.weighted.mean(wage, weight)`
[1] 75.6041053069

$`B.weighted.mean(wage, weight)`
[1] 70.2265239366