“未找到”重量尝试使用dplyr summarize_all

时间:2017-09-29 13:34:04

标签: r dplyr

我正在尝试使用dplyr和summarize_all计算加权平均值。我见过类似的问题,特别是这篇文章: How do I compute weighted average using summarise_each?

这看起来非常简单,但我一直收到对象'权重'未找到错误消息。

这是一个可重复的例子:

data <- tibble::tribble(~group, ~weight, ~x1, ~x2,
                    1, 1, 3, 2,
                    1, 1, 4, 7,
                    1, 1, 1, 4,
                    2, 1, 2, 2,
                    2, 1, 5, 3,
                    2, 1, 4, 2)

# Just regular means
data %>%
      dplyr::group_by(group) %>%
      dplyr::summarize_all(mean)

# First attempt
data %>%
      dplyr::group_by(group) %>%
      dplyr::summarize_all(weighted.mean(., weight))

# Second attempt based on syntax from post above
data %>%
      dplyr::group_by(group) %>%
      dplyr::summarize_all(funs(weighted.mean(., weight)), -weight)

当我运行此代码时会发生这种情况:

> data %>%
+   dplyr::group_by(group) %>%
+   dplyr::summarize_all(mean)
# A tibble: 2 x 4
  group weight       x1       x2
  <dbl>  <dbl>    <dbl>    <dbl>
1     1      1 2.666667 4.333333
2     2      1 3.666667 2.333333
> 
> data %>%
+   dplyr::group_by(group) %>%
+   dplyr::summarize_all(weighted.mean(., weight))
Error in weighted.mean.default(., weight) : object 'weight' not found
> 
> data %>%
+   dplyr::group_by(group) %>%
+   dplyr::summarize_all(funs(weighted.mean(., weight)), -weight)
Error in eval_bare(dot$expr, dot$env) : object 'weight' not found

非常感谢任何关于这种情况发生的信息以及我能做些什么(如果有的话)。

谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于,使用summarize_all您还要尝试计算weight列的加权平均值。

您可能对summarise_at

更感兴趣
data %>%
  dplyr::group_by(group) %>%
  dplyr::summarize_at(vars(x1, x2), funs(weighted.mean(., weight)))

# A tibble: 2 x 3
  group       x1       x2
  <dbl>    <dbl>    <dbl>
1     1 2.666667 4.333333
2     2 3.666667 2.333333