R/rescale_yes_no_3.R

#' Rescale 3-level yes-no questions.
#'
#' Rescale Eurobarometer trust in institutions variable with two levels,
#' \code{"Yes, on several occasions"} and \code{"Yes, once or twice"} and
#' \code{"No"}.
#' This is a wrapper function around the \code{\link{rescale_categories}} function.
#' @param column A column from a survey data frame where trust in an institution
#' is recorded.
#' @param from Defaults \code{"Yes, on several occasions"} and \code{"Yes, once or twice"} and
#' \code{"No"}.
#' @param to Defaults to \code{c(2,1,0)}. If  \code{return_class = "character"} or
#' \code{return_class = "factor"} is selected it returns the abbreviated category
#' names \code{c("several_occasions", "once_or_twice", "no")}.
#' @param na_labels  Defaults to \code{"default"}.
#' @param exact_from Deafults to \code{TRUE}. If \code{FALSE} you can use the
#' partial matching, but beware that in this case, \code{"twenty"} will be replaced by
#' @param return_class Default is \code{"numeric"}, alternatives \code{"character"} or
#' \code{"factor"}.
#' @param underscore Defaults to \code{TRUE} in which case factor names or character strings
#' contain underscore_between_words.  This is a better approach for further programming,
#' but you can choose \code{FALSE} for nicer printing results. See examples.
#' @examples
#' rescale_yes_no_3  (column =
#'    c("Yes, on several occasions",
#'      "Yes, once or twice",
#'      "No", "NA", "DK"),
#'      na_labels = "default",
#'      underscore = FALSE,
#'      return_class = "character")
#'
#' rescale_yes_no_3  (column =
#'    c("Yes, on several occasions",
#'      "Yes, once or twice",
#'      "No", "NA", "DK"),
#'      na_labels = "default",
#'      return_class = "numeric")
#' @export

rescale_yes_no_3 <- function ( column,
                                 from = c("Yes, on several occasions",
                                          "Yes, once or twice",
                                          "No"),
                                 to = c(2,1,0),
                                 na_labels = "default",
                                 underscore = TRUE,
                                 exact_from = TRUE,
                                 return_class = "numeric") {
  if ( return_class %in% c("character", "factor")) {
    to <- c("several_occasions", "once_or_twice", "no")
    if (underscore == FALSE) {
      to <- gsub("_", " ", to)
    }
  }

  return(surveyreader::rescale_categories(column = column,
                            from = from, to = to,
                            na_labels = na_labels,
                            exact_from = exact_from,
                            return_class = return_class))

}
antaldaniel/surveyreader documentation built on May 16, 2019, 2:29 a.m.