I am using the qwraps2 library to produce a summary table. When writing my list of summary statistics, I have:
summary1 <-
list("Wage" =
list("min" = ~ min(WAGE),
"max" = ~ max(WAGE),
"mean (sd)" = ~ qwraps2::mean_sd(WAGE)))
which is the standard set-up from the manual. However, I would like to calculate the weighted mean and then the standard deviation. Is there a way to do this is qwraps2 or with another command/ library?
EDIT
Suppose following dataframe of 5 people, their hourly wage, and their weight in the dataset (for sampling purposes, since I am working with CPS data).
library(qwraps2)
person <- c(1, 2, 3, 4, 5)
wage <- c(20, 25, 30, 35, 40)
weight <- c(1, 1.5, 2, 2.5, 3)
dfa <- as.data.frame(cbind(person, wage, weight))
dfa
The arithmetic mean (calculated in qwraps2) is 30: arithmetic mean
The weighted mean (which is what I want to calculate in summary1
is 32.5:
weighted mean
summary1 <-
list("Wage" =
list("min" = ~ min(wage),
"max" = ~ max(wage),
"mean (sd)" = ~ qwraps2::mean_sd(wage)))
output <-summary_table(dfa, summary1)
output
Here, the output for the mean is 30 (using qwraps2), which is the arithmetic mean. However, I want the output to be the weighted mean of 32. I hope this helps!
There is a base R
weighted.mean
which does give the output as expected
summary1 <-
list("Wage" =
list("min" = ~ min(WAGE),
"max" = ~ max(WAGE),
"mean (sd)" = ~ weighted.mean(WAGE, weight)))
output <- summary_table(dfa, summary1)
output
\begin{tabular}{l|l}
\hline
& dfa (N = 5)\\
\hline
\bf{Wage} & ~\\
\hline
~~ min & 20\\
\hline
~~ max & 40\\
\hline
~~ mean (sd) & 32.5\\
\hline
\end{tabular}