【发布时间】:2019-01-28 04:22:00
【问题描述】:
我想计算一个巨大数据集的加权平均值。
我需要的是以下(每一行),我有NAs,
所以我需要以某种方式合并na.rm = TRUE。
我希望计算以下内容(距离 1 到距离 10):
(distance1 * X1CityNumber + ... + distance10 * X10CityNumber) /
(X1CityNumber + ... + X10CityNumber)
我编写了以下代码,但它产生了错误的数字。
for (i in 1:378742) {
rcffull$distance[i] <- weighted.mean(cbind(rcffull$distance1[i],
rcffull$distance2[i],
rcffull$distance3[i],
rcffull$distance4[i],
rcffull$distance5[i],
rcffull$distance6[i],
rcffull$distance7[i],
rcffull$distance8[i],
rcffull$distance9[i],
rcffull$distance10[i]),
cbind(rcffull$X1CityNumber[i],
rcffull$X2CityNumber[i],
rcffull$X3CityNumber[i],
rcffull$X4CityNumber[i],
rcffull$X5CityNumber[i],
rcffull$X6CityNumber[i],
rcffull$X7CityNumber[i],
rcffull$X8CityNumber[i],
rcffull$X9CityNumber[i],
rcffull$X10CityNumber[i]),
na.rm = TRUE)
}
有什么建议吗?
列数较少的样本数据:
distance1 Weights1 distance2 Weights2
1 5 3 8 2
2 NA 2 3 3
3 5 NA 4 4
#desired output:
Mean distance
1 6.2 #= (5 * 3 + 8 * 2) / (3 + 2)
2 3.0 #= (3 * 3) / 3
3 3.0 #= (4 * 4) / 4
【问题讨论】:
标签: r dataframe matrix mean weighted