--- title: "wtdFreqTable" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{wtdFreqTable} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(coreStatsNMR) library(dplyr) set.seed(1234) ``` `wtdFreqTable` is a function for calculating the frequency any number of groups in a dataset, with or without weights. It takes up to 4 input values: - a dataframe (or data.table) - two variables for grouping the dataset, and - an (optional) weights column ### Sample data For testing, we use the `mtcars` dataset that comes stock with R. It contains data from the 1974 Motor Trend US magazine, with fuel economy and other statistics for a set of 32 cars. It looks something like this: ```{r mtcars} glimpse(mtcars) ``` We add random weights to the data for testing below. ```{r test_data, echo=TRUE} set.seed(1234) rand_wt <- data.frame(rand_bin = 1:3, rand_wt = rnorm(3, mean = 1, sd = 0.333)) test_data <- mtcars %>% mutate(index = row_number(), rand_bin = sample(1:3, nrow(mtcars), replace = TRUE)) %>% merge(rand_wt, by = "rand_bin") ``` ### Changing weights The code chunk below applies `wtdFreqTable` to the test data, comparing frequencies when weighting either by miles per gallon (`mpg`) or the random weight variable we created (`rand_wt`). Switching the weighting scheme only changes the weighted overall frequencies (in the right column). **`wtdFreqTable` does not normalize the weights to make total weighted count the same as the unweighted one, hence the wonky results when weighting by mpg and hp** ```{r wtdFreq_0} wtdFreqTable(test_data, x = "cyl", y = "gear", totWeightVar = "mpg") %>% knitr::kable() wtdFreqTable(test_data, x = "cyl", y = "gear", totWeightVar = "mpg", inGroupWeightVar = "rand_wt", accuracy = 0.01) %>% knitr::kable() wtdFreqTable(test_data, x = "cyl", y = "gear", totWeightVar = "rand_wt") %>% knitr::kable() wtdFreqTable(test_data, x = "cyl", y = "gear", totWeightVar = "rand_wt", inGroupWeightVar = "rand_wt", accuracy = 0.01) %>% knitr::kable() ``` ```{r wtdFreq_1} wtdFreqTable(mtcars, x = "cyl", y = "gear", totWeightVar = "hp", tot.label = "Statewide") %>% knitr::kable() ```