[英]Calculate the weighted mean for a variable, which has two types with R
I have the following dataset with the number of times ("n") the variable "V0220" appears in the data by the "id_municipio", but this variable has two types: 1 and 2. Moreover, I have the weight ("peso_amostral") of each observation.我有以下数据集,其中变量“V0220”通过“id_municipio”出现在数据中的次数(“n”),但这个变量有两种类型:1 和 2。此外,我有权重(“peso_amostral ") 的每个观察。
id_municipio peso_amostral v0220 n
1100015 2.04 2 1
1100015 2.68 1 1
1100015 3.45 2 1
1100015 4.51 1 1
1100015 4.62 2 1
1100015 4.75 1 1
What I would like to do is the following:我想做的是以下几点:
id_municipio 2 1
1100015 X Y
Therefore, I want to calculate the weighted mean for each variable "V0220" for the type (2 or 1) of this variable by id_municipio.因此,我想通过 id_municipio 计算该变量的类型(2 或 1)的每个变量“V0220”的加权平均值。 Note that "X" and "Y" are the weighted mean values for "V0220", by type 2 and 1, respectively.
请注意,“X”和“Y”是“V0220”的加权平均值,分别按类型 2 和 1。 I want to do it using R.
我想用 R 来做。
You can try this using dcast
from data.table
.您可以使用
dcast
中的data.table
尝试此操作。 You can change fun.aggregate
for the function that you need.您可以更改
fun.aggregate
为您需要的功能。
library(data.table)
dcast(data,
id_municipio ~ v0220,
fun.aggregate = mean,
value.var = "peso_amostral")
OUTPUT:输出:
id_municipio 1 2
1 1100015 3.98 3.37
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.