Frequency tables with weighted data in R

34.5k views Asked by At

I need to calculate the frequency of individuals by age and marital status so normally I'd use:

    table(age, marital_status)

However each individual has a different weight after the sampling of the data. How do I incorporate this into my frequency table?

6

There are 6 answers

1
Victorp On BEST ANSWER

You can use function svytable from package survey, or wtd.table from rgrs.

EDIT : rgrs is now called questionr :

df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40))

library(questionr)
wtd.table(x = df$var, weights = df$wt)
#  A  B 
# 40 60

That's also possible with dplyr :

library(dplyr)
count(x = df, var, wt = wt)
# # A tibble: 2 x 2
#        var     n
#     <fctr> <dbl>
#   1      A    40
#   2      B    60
0
s_baldur On

Using data.table you could do:

# using the same data as Victorp
setDT(df)[, .(n = sum(wt)), var] 

   var  n
1:   A 40
2:   B 60
0
Carlos Hernández On

Another solution from package expss:

    df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40))
    
    library(expss)
    
    fre(df$var, weight = df$wt)

 | df$var | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
 | ------ | ----- | ------------- | ------- | ------------ | ----------------------- |
 |      A |    40 |            40 |      40 |           40 |                      40 |
 |      B |    60 |            60 |      60 |           60 |                     100 |
 | #Total |   100 |           100 |     100 |          100 |                         |
 |   <NA> |     0 |               |       0 |              |                         |

1
Santiago Capobianco On

You can also use tablefreq from package freqweights:

df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40))

library(freqweights)

tablefreq(df, "var", "wt")

A tibble: 2 x 2
var    freq
<fct> <dbl>
1 A        40
2 B        60
0
Markm0705 On

Use package weights and function wpct

require(weights)
df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40))
wpct(df$var, df$wt)

 A   B 
0.4 0.6 
0
Sicabí On

Just for the sake of completeness, using base R:

df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40))

aggregate(x = list("wt" = df$wt), by = list("var" = df$var), FUN = sum)

var wt
1 A 40
2 B 60

Or with the less cumbersome formula notation:

aggregate(wt ~ var, data = df, FUN = sum)

var wt
1 A 40
2 B 60