R/get_mcsopct.R

#' Gets percentage from MCSO question
#' 
#' Gets percentage of values in a MCSO question with specific value. It is
#' more often used for data checking or reporting single numbers in a document.
#' 
#' @param item Data vector from MCSO question
#' @param value The particular value being matched
#' @param digits The number of digits after the decimal for rounding the
#' percent
#' 
#' @examples 
#' # Gets the percent of people who answered "Male" as an option
#' get_mcmopct(survey$Q4, "Male")
#' # Rounds to the nearest integer
#' get_mcmopct(survey$Q4, "Male", digits=0)
#' 
#' @export
get_mcsopct <- function(item, value, digits=2) {
  
  # Returns percentage
  return(as.numeric(round(digits=digits, 100*prop.table(table(item))[value])))
}
rblissett/pubpub documentation built on May 29, 2019, 6:53 p.m.