R/rescale_eu2020_goal.R

#' Rescale goals Eurobarometer question
#'
#' For example, to rescale the standard Eurobarometer EU 2020 goals variable(s) with
#' categories \code{"Too ambitious"}, \code{"About right"},
#' \code{"Too modest"}.
#' This is a wrapper function around the \code{\link{rescale_categories}} function.
#' @param column A column from a survey data frame where the situtation is recorded with
#' four categories.
#' @param from Defaults to \code{c("Very good", "Fairly good",
#' "Rather bad", "Very bad")}.
#' @param to Defaults to \code{c(-1,0,1)}. If  \code{return_class = "character"} or
#' \code{return_class = "factor"} is selected it returns the abbreviated category
#' names \code{c("too_ambitious", "about_right", "too_modest"}.
#' @param na_labels  Defaults to \code{"default"}.
#' The pre-coded profiles can be accessed via \code{\link{get_na_labels}}.
#' @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_eu2020_goals (
#'   column = c("Too ambitious", "Too modest",
#'             "Inap", "About right"),
#'   return_class = "character",
#'   underscore = FALSE)
#'
#' rescale_eu2020_goals (c("Too ambitious", "Too modest",
#'                         "Inap", "About right"))
#' @export

rescale_eu2020_goals <- function ( column,
                                 from = c("Too ambitious",
                                          "About right",
                                          "Too modest"),
                                 to = c(-1,0,1),
                                 na_labels = "default",
                                 return_class = "numeric",
                                 underscore = TRUE) {

  if ( return_class %in% c("character", "factor")) {
    to = c("too_ambitious", "about_right", "too_modest")
  }

  if (underscore == FALSE) {
    to = gsub("_", " ", to)
    }

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

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