R/rescale_political_interest.R

#' Rescale level of political interest
#'
#' Create a uniform variable from four categories such as
#' "Not at all interested in politics", "Slightly interested in politics",
#' "Moderately interested in politics", "Strongly interested in politics".
#' This is a wrapper function around the \code{\link{rescale_categories}} function.
#' @param column A column from a survey data with a four-level scale.
#' @param from Defaults to \code{c("Not at all interested in politics",
#' "Slightly interested in politics", "Moderately interested in politics",
#'  "Strongly interested in politics")}.
#' @param to Defaults to \code{NULL} which gives a shorthened version of the labels
#' if the return_class is \code{"character"} or \code{"factor"} and returns
#' \code{c(0,1,2,3)} if it is \code{"numeric"}.
#' @param na_labels  Defaults to \code{c("DK")}.
#' @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{"factor"}, alternatives \code{"character"} or
#' \code{"numeric"}.
#' @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_political_interest (c("Not at all interested in politics",
#'                              "Slightly interested in politics",
#'                              "DK",
#'                              "Moderately interested in politics",
#'                              "Strongly interested in politics"),
#'                            return_class = "numeric")
#'
#'rescale_political_interest (c("Not at all interested in politics",
#'                              "Slightly interested in politics",
#'                              "DK",
#'                              "Moderately interested in politics",
#'                              "Strongly interested in politics"),
#'                              underscore = FALSE)
#' @export

rescale_political_interest <- function ( column,
                          from = c("Not at all interested in politics",
                                   "Slightly interested in politics",
                                   "Moderately interested in politics",
                                   "Strongly interested in politics"),
                          to = NULL,
                          na_labels = c("DK"),
                          exact_from = TRUE,
                          return_class = "factor",
                          underscore = TRUE) {
  if (is.null(from)) {
    from = c("Not at all interested in politics",
             "Slightly interested in politics",
             "Moderately interested in politics",
             "Strongly interested in politics")
  }

  if(is.null(to)) {

    if ( return_class == "numeric") {
      to = c(0,1,2,3)

    } else {
      to =      c("not_interested",
                  "slightly_interested",
                  "moderately_interested",
                  "strongly_interested")
    }

  }

  if ( underscore == FALSE ) {
    to <- gsub("_", " ", to)
  }
  return(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.