Frequency tables with weighted data in R

user2568648 picture user2568648 · Sep 3, 2013 · Viewed 20.1k times · Source

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?

Answer

Victorp picture Victorp · Sep 3, 2013

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