1

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)
jay.sf
  • 60,139
  • 8
  • 53
  • 110
SpecialK201
  • 111
  • 7

2 Answers2

2

You can use xtabs and convert as.data.frame so you don't need to load packages. Then just cbind the proportions. Valid percentages are calculated without the NAs.

tbl <- as.data.frame(xtabs(wght ~ a, df, addNA=TRUE))

cbind(tbl, perc=proportions(tbl$Freq)*100, valid_perc=c(proportions(na.omit(tbl)$Freq), NA)*100)
#      a Freq     perc valid_perc
# 1    1  3.1 27.67857   32.97872
# 2    2  2.7 24.10714   28.72340
# 3    3  3.6 32.14286   38.29787
# 4 <NA>  1.8 16.07143         NA

Data:

df <- structure(list(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)), class = "data.frame", row.names = c(NA, 
-12L))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
2

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
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51