为不同权重的组计算R中的一系列加权均值 [英] Calculate a series of weighted means in R for groups with different weightings
问题描述
我有以下数据集(我的实际数据的简单版本)数据",并想分别使用权重w1和w2计算变量x1和x2的加权均值,分为两个组(由变量n).
I have the following dataset (simple version of my actual data), 'data', and would like to calculate weighted means for variables x1 and x2, using weightings w1 and w2 respectively, split up into two groups (groups determined by the variable n).
data <- data.frame(n = c(1,1,1,2,2,2), x1 = c(4,5,4,7,5,5), x2 = c(7,10,9,NaN,11,12), w1 = c(0,1,1,1,1,1), w2 = c(1,1,1,0,0,1))
我正在尝试使用with()来执行此操作,但是在运行此命令时出现错误:
I'm trying to do it using with() but get an error when I run this:
with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n = n), FUN = weighted.mean, w = list(w1 = w1,w2 = w2)))
另一方面,如果未指定权重,则可以使用,但是在这种情况下,将使用默认级别的权重(即与使用FUN = mean相同).
On the otherhand, if weights aren't specified it works, but in this case default level weights are used (i.e. same as using FUN=mean).
with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n = n), FUN = weighted.mean))
此问题类似于按组和列的加权均值,除了我的问题包括针对不同列的不同权重.我尝试使用数据表,但是它遇到了与with()相同的加权错误. 预先感谢您的帮助.
This question is similar to weighted means by group and column, except that my question includes different weightings for different columns. I tried using a data table but it runs into the same weighting errors as with(). Thanks in advance for any help.
推荐答案
尝试
library(data.table)
setDT(data)[, .(x1=weighted.mean(x1, w1), x2=weighted.mean(x2, w2)) , by = n]
或者如@thelatemail所评论的那样,我们可以使用Map
循环遍历"x",对应的"w's"列,并使用单个weighted.mean
Or as @thelatemail commented, we can use Map
to loop over "x's", corresponding "w's" columns and call with a single weighted.mean
setDT(data)[, Map(weighted.mean, list(x1,x2), list(w1,w2)), by = n]
如果有很多"x"和"w"列,我们可以使用grep
获取列名,使用mget
返回Map
If there are many "x" and "w" columns, we can use grep
to get the column names, mget
to return the values inside the Map
setDT(data)[, Map(weighted.mean, mget(grep('x', names(data),
value=TRUE)), mget(grep('w', names(data), value=TRUE))), by = n]
这篇关于为不同权重的组计算R中的一系列加权均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!