I am trying to build a function to obtain a weighted mean at the same variable in different dataframes in a list. The function is not taking some arguments (wage and weight), I believe there is a "" or [[]] problems but I can't seem to make it work.
Here's the reproducible example that gives me the error
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))
Thanks!