R/eda_freqtable.R

Defines functions eda_freqtable

Documented in eda_freqtable

#' Calculates a frequency table for a single survey item
#'
#' @param data a \code{data.frame} object
#' @param item a survey item
#' @param rm.dk a logical value indicating whether "Don't know" values (-8) should be stripped before the computation proceeds
#'
#' @return a frequency table (in percent)
#' @export
#'
#' @examples
#' \dontrun{
#' eda_freqtable(data = w5, item = w5_q11, rm.dk = FALSE)
#' }
#' @import dplyr
eda_freqtable <- function(data, item, rm.dk = FALSE){
  if (!is.data.frame(data)) stop("Make sure the input is a data.frame")
  if (isTRUE(rm.dk))  {
    df <- filter(data, {{item}} >= 0)
  } else  {
    df <- filter(data, {{item}} >= -8)
  }

  vec <- pull(df, {{item}})
  table <- round(prop.table(table(vec, dnn = "Relative frequencies:")), digits = 4)
  table*100
}
bonschorno/sep documentation built on April 1, 2022, 11:37 a.m.