Search code examples
rdatabasecastingreshapemelt

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.

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. Note that "X" and "Y" are the weighted mean values for "V0220", by type 2 and 1, respectively. I want to do it using R.


Solution

  • You can try this using dcast from data.table. You can change fun.aggregate for the function that you need.

    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