Complex survey tables in R

27 Views Asked by At

I need to create a table with data from a complex survey (with weights). Which function can be used to produce a table where we have for example one categorical variable in column, two or three categorical variables in row. Next, display the percentages (proportions) of another dichotomous variable in the table cells.

What package can I use?

1

There are 1 best solutions below

0
Alex Strashny On

https://cdcgov.github.io/surveytable/

In your data frame, be sure that categorical variables are stored as factor. Then, convert it to a survey object using a command like this:

mysurvey = survey::svydesign(weights = ~ WeightsVariable, data = mydf)

If your survey has other attributes, such as clusters and strata, you need to specify those in this command as well.

Then, you can create tables very easily like so:

library(surveytable)
set_survey(mysurvey, opts = "general")
tab("NameOfVariable")
tab_cross("Var1", "Var2")

and so on. See here for an intro: https://cdcgov.github.io/surveytable/articles/surveytable.html