ishido ishido - 1 month ago 10
R Question

How to use sample weights with categorical data for visualization?

If one has a data set that looks as follows:

data <- data.frame()
data$gender <- c(F,F,F,M,F)
data$province <- c(G,N,K,L,K)
data$volume <- c(500,750,1250,NA,300)
data$weight <- c(1.36,0.67,5.22,1.66,1.14)


How do I apply these weights to the data set? Is it enough to just multiply the sample weights with the numerical variables or do the categorical variables also have to be weighted?

Answer

The questionr and survey packages handle weighting. Here's an example:

library("questionr")
table <- wtd.table(data$gender, weights = data$weight)
barplot(as.matrix(table))