dplyr加权平均值会产生长度错误...如何总结加权平均值?

时间:2019-01-08 00:50:25

标签: r dplyr statistics

如何获得加权调查结果的摘要?

每次尝试时,我都会收到一条错误消息,说长度不一样。

library(tidyverse)

x <- tbl_df(data.frame("Age" = c(21,21,15,15,22,22,47,47,42,42,33,33,32,32), 
"survey answer 1-10 scale" = c(1,10,3,6,5,4,8,1,10,3,6,5,4,8), 
"weight" =c(.7,.8,.9,1,1.1,1.2,1.3,.7,.8,.9,1,1.1,1.2,1.3)))

print(x)

# # A tibble: 14 x 3
#      Age survey.answer.1.10.scale weight
#    <dbl>                    <dbl>  <dbl>
#  1    21                        1    0.7
#  2    21                       10    0.8
#  3    15                        3    0.9
#  4    15                        6    1  
#  5    22                        5    1.1
#  6    22                        4    1.2
#  7    47                        8    1.3
#  8    47                        1    0.7
#  9    42                       10    0.8
# 10    42                        3    0.9
# 11    33                        6    1  
# 12    33                        5    1.1
# 13    32                        4    1.2
# 14    32                        8    1.3



x %>%
    group_by(Age) %>%
    summarise(weighted.mean(., w=.$weight, na.rm=TRUE))

哪个返回:

Error in summarise_impl(.data, dots) : 
Evaluation error: 'x' and 'w' must have the same length.

另一个回答是,加权意味着仅适用于矩阵?但这没有意义。即使尝试了,也没有布宜诺斯艾利斯:

as.matrix(x)->mat.rx
mat.rx %>%
group_by(Age) %>%
    summarise(weighted.mean(., w=.$weight, na.rm=TRUE))

返回:

Error in UseMethod("group_by_") : 
  no applicable method for 'group_by_' applied to an object of class "c('matrix'
, 'double', 'numeric')"

1 个答案:

答案 0 :(得分:0)

library(tidyverse)

x <- tbl_df(data.frame("Age" = c(21,21,15,15,22,22,47,47,42,42,33,33,32,32), 
"survey answer 1-10 scale" = c(1,10,3,6,5,4,8,1,10,3,6,5,4,8), 
"weight" =c(.7,.8,.9,1,1.1,1.2,1.3,.7,.8,.9,1,1.1,1.2,1.3)))

x %>% 
    group_by(Age) %>%
    summarise(weighted.mean(survey.answer.1.10.scale, w=weight, na.rm=TRUE))
# A tibble: 7 x 2
    Age `weighted.mean(survey.answer.1.10.scale, w = weight, na.rm = TRUE)`
  <dbl>                                                               <dbl>
1    15                                                                4.58
2    21                                                                5.8 
3    22                                                                4.48
4    32                                                                6.08
5    33                                                                5.48
6    42                                                                6.29
7    47                                                                5.55