[英]Use column name as weight to calculate weighted mean in R
Continue from my previous (answered) question . 继续我先前的问题 (已回答)。
Say I have this data, 说我有这个数据,
> df
rank1 rank2 rank3 rank4 rank5
1 A B C D E
2 A C B D E
3 C A B E D
4 B A C D E
5 A B D C E
I managed to create a frequency table of ranking by item (thanks to akrun ), 我设法创建了一个按项目排名的频率表(感谢akrun ),
> df.frequency
ranking
items 1 2 3 4 5
A 3 2 0 0 0
B 1 2 2 0 0
C 1 1 2 1 0
D 0 0 1 3 1
E 0 0 0 1 4
> str(df.frequency)
'table' int [1:5, 1:5] 3 1 1 0 0 2 2 1 0 0 ...
- attr(*, "dimnames")=List of 2
..$ items : chr [1:5] "A" "B" "C" "D" ...
..$ ranking: chr [1:5] "1" "2" "3" "4" ...
In Excel, I use =SUMPRODUCT($B$1:$F$1,B2:F2)/SUM(B2:F2)
to get the weighted mean, 在Excel中,我使用
=SUMPRODUCT($B$1:$F$1,B2:F2)/SUM(B2:F2)
获得加权平均值,
1 2 3 4 5 Mean
A 3 2 0 0 0 1.4
B 1 2 2 0 0 2.2
C 1 1 2 1 0 2.6
D 0 0 1 3 1 4
E 0 0 0 1 4 4.8
In R, How to I calculate the weighted mean of each item where the weight is the rank? 在R中,如何计算权重为等级的每个项目的加权平均值? I want to calculate SD and median as well.
我也想计算SD和中位数。
Are you looking for something simple like this: 您是否正在寻找像这样的简单东西:
> a<-1:dim(df)[1] ### colnames
> z<-0
> b<-apply(df,1,function(x) x/sum(x)) ### ratio
> for(i in 1:dim(df)[1]){
+ z[i]<-sum(a*b[i,]) ### column weighted ratio
+ }
> z
[1] 1.4 2.2 2.6 4.0 4.8
If you want to add it to the column just cboi 如果您想将其添加到列中,只需cboi
> cbind(x,z)
1 2 3 4 5 z
1 3 1 1 0 0 1.4
2 2 2 1 0 0 2.2
3 0 2 2 1 0 2.6
4 0 0 1 3 1 4.0
5 0 0 0 1 4 4.8
Inspired by @TonyHellmuth's solution, this can be also solved by 受到@TonyHellmuth解决方案的启发,这也可以通过解决
cbind(tbl, z= c(seq_len(dim(tbl)[1])%*% t(tbl)/rowSums(tbl)))
# 1 2 3 4 5 z
#A 3 2 0 0 0 1.4
#B 1 2 2 0 0 2.2
#C 1 1 2 1 0 2.6
#D 0 0 1 3 1 4.0
#E 0 0 0 1 4 4.8
tbl <- table(unlist(df), c(col(df)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.