rweightedfrequency-table

How do I obtain a weighted frequency table with proportions?


I'm working on a dataset that includes a post-stratification weight, and I am looking for a way to get more info about specific variables after they have been weighted, but I am struggling.

Here's a sample dataframe:

a <- c(1, 3, 2, 1, 2, 2, 3, 3, 1, 3, NA, NA)
wght <- c(0.8, 0.9, 1.2, 1.5, 0.5, 1, 0.7, 0.9, 0.8, 1.1, 1, 0.8)
df <- data.frame(a, wght)

Column a contains the coded responses to a question (say agree/neutral/disagree), and wght contains the weight. I found a way to display the weighted number of observations:

library(magrittr)
df %>% dplyr::count(a, wt=wght)

I would now like to get the info on this distribution that I could get with freq from the descr package (especially percentage and valid percentage). I tried various things, such as the following, but it produces a strange frequency table.

dfwt <- df %>% count(a, wt=wght)
freq(dfwt$a)

Solution

  • freq from {descr} is used as following:

    df$a <- factor(df$a, levels = c(1, 2, 3), labels = c("agree", "neutral", "disagree"))
    
    descr::freq(df$a, df$wght, plot = FALSE)
    
    # df$a 
    #          Frequency Percent Valid Percent
    # agree          3.1   27.68         32.98
    # neutral        2.7   24.11         28.72
    # disagree       3.6   32.14         38.30
    # NA's           1.8   16.07              
    # Total         11.2  100.00        100.00