1

I'm using the R package table1 to create a simple table of summary statistics for mostly factored variables (age categories, sex, race, etc.). The counts in the table need to be raw counts, but the percentages need to be weighted by a survey weight variable in the dataset. It seems like a simple enough task that there should be a table-making package that has a function for this, but I can't seem to find a solution in table1, flextable, gt, kableExtra, or any other package.

The example below shows roughly what I've been able to do so far, but the percentages that appear are unweighted and I can't find a way to use the sv_weight variable to calculate weighted percentages.

set.seed(123)
dat <- data.frame(
  year = factor(sample(c("2019", "2020"), 100, replace = TRUE)),
  sv_weight = (sample(1:150, 100, replace = TRUE)),
  sex = factor(sample(c("Male", "Female"), 100, replace = TRUE)),
  race =  factor(sample(c("White", "Hispanic", "Black"), 100, replace = TRUE)))

library(table1)
tab<- table1(~ sex + race | year, data = dat) 

tab
AndrewGB
  • 16,126
  • 5
  • 18
  • 49

1 Answers1

2

You can use a combination of the survey and gtsummary packages. There is an option in survey::svydesign to add weights. Then, the survey object is piped into tbl_svysummary. However, depending on your expected output, you might need to use a different statistic or adjust some of the other settings.

library(gtsummary)
library(dplyr)

results <-
  survey::svydesign(~ 1, data = dat, weights = ~ sv_weight) %>%
  tbl_svysummary(
    by = year,
    include = c(sex, race),
    statistic = list(all_categorical() ~ "{n_unweighted} ({p}%)")
  )

Output

enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • Thank you so much! This has been so helpful. The thing I'm stuck on now is how to format missingness. I have a row within each categorical variable for the count of missing values in that variable - this appears automatically. But it looks like the count is weighted, rather than unweighted, even though the counts in other rows are unweighted. Do you know how to format the rows for missing? – notasfarwest May 02 '22 at 20:40