Search code examples
rpercentagesurvey

Calculate percentage/frequency of a value in a survey object


I have a national survey composed of many variables, like this one (for the sake of semplicity I omitted some variables):

year  id  y.b   sex   income  married   pens   weight
2002  1   1950   F    100000     1       0      1.12
2002  2   1943   M    55000      1       1      0.55
2004  1   1950   F    88000      1       1      1.1
2004  2   1943   M    66000      1       1      0.6
2006  3   1966   M    12000      0       1      0.23
2008  3   1966   M    24000      0       1      0.23
2008  4   1972   F    33000      1       0      0.66
2010  4   1972   F    35000      1       0      0.67

Where id is the person interviewed, y.b is year of birth, married is a dummy (1 married, 0 single), pens is a dummy that takes value one if the person invest in a complementary pension form; weight are the survey weights.

Consider that the original survey is made up to 40k observations from 2002 to 2014(I filtered it in order to have only individuals that appear more than one time). I use this command to create a survey object:

d.s <- svydesign(ids=~1, data=df, weights=~weight)

Now that the df is weighted I want to find for example the percentage of women or the percentage of married person that invest in complementary pension; I read on R help and on the web to find a command to get the percentage but I didn't find the right one.


Solution

  • # same setup
    library(survey)
    
    df <- data.frame(sex = c('F', 'M', 'F', 'M', 'M', 'M', 'F', 'F'),
                    married = c(1,1,1,1,0,0,1,1),
                    pens = c(0, 1, 1, 1, 1, 1, 0, 0),
                    weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))
    
    d.s <- svydesign(ids=~1, data=df, weights=~weight)
    
    # subset to women only then calculate the share with a pension
    svymean( ~ pens , subset( d.s , sex == 'F' ) )