如何计算R中的加权均值?

加权平均值是平均值,该平均值是通过找到权重乘积与值的乘积之和然后将其除以总权重之和而确定的。如果权重是成比例的,那么权重的总和应为1。在基数R中,我们有一个weighted.mean函数来找到加权均值,我们只需要传递值向量和权重向量即可。

例子

x1<-c(1,2,3,4)
x1

输出结果

[1] 1 2 3 4

示例

Weight_x1<-c(0.25,0.25,0.25,0.25)
Weight_x1

输出结果

[1] 0.25 0.25 0.25 0.25

示例

Weighted_Mean_x1<-weighted.mean(x1,Weight_x1)
Weighted_Mean_x1

输出结果

[1] 2.5

示例

x2<-rpois(10,5)
x2

输出结果

[1] 3 3 4 6 6 10 5 3 3 6

 例

Weight_x2<-rep(0.1,10)
Weight_x2

输出结果

[1] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1

示例

Weighted_Mean_x2<-weighted.mean(x2,Weight_x2)
Weighted_Mean_x2

输出结果

[1] 4.9

示例

x3<-c(8,6,7)
x3

输出结果

[1] 8 6 7

示例

Weight_x3<-c(0.5,0.3,0.2)
Weight_x3

输出结果

[1] 0.5 0.3 0.2

示例

Weighted_Mean_x3<-weighted.mean(x3,Weight_x3)
Weighted_Mean_x3

输出结果

[1] 7.2

示例

x4<-rnorm(5,1.5)
x4

输出结果

[1] 3.0320687 0.9741989 0.2783473 0.8061909 0.5833953

示例

Weight_x4<-rep(0.2,5)
Weight_x4

输出结果

[1] 0.2 0.2 0.2 0.2 0.2

示例

Weighted_Mean_x4<-weighted.mean(x4,Weight_x4)
Weighted_Mean_x4

输出结果

[1] 1.13484

示例

x5<-c(1,2,5,7)
x5

输出结果

[1] 1 2 5 7

示例

Weight_x5<-c(2,14,8,32)
Weight_x5

输出结果

[1] 2 14 8 32

示例

Weighted_Mean_x5<-weighted.mean(x5,Weight_x5)
Weighted_Mean_x5

输出结果

[1] 5.25

示例

x6<-runif(10,2,4)
x6

输出结果

[1] 2.126860 3.194281 3.051176 3.083388 3.393485 2.139493 2.634237 2.647985
[9] 3.318430 2.765557

示例

Weight_x6<-rep(0.1,10)
Weight_x6

输出结果

[1] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1

示例

Weighted_Mean_x6<-weighted.mean(x6,Weight_x6)
Weighted_Mean_x6

输出结果

[1] 2.835489

示例

x7<-c(1,2,3,4)
x7

输出结果

[1] 1 2 3 4

示例

Weight_x7<-c(73,378,459,90)
Weight_x7

输出结果

[1] 73 378 459 90

示例

Weighted_Mean_x7
<-weighted.mean(x7,Weight_x7)
Weighted_Mean_x7

输出结果

[1] 2.566