R/rescale_outlook.R

#' Rescale outlook Eurobarometer questions
#'
#' Rescale the standard Eurobarometer outlook variable(s) with
#' categories \code{"Better"}, \code{"The same"},  \code{"Worse"}.
#' The generic question is \code{"What are your expectations for
#'  the next twelve months: will the next twelve months be better,
#' worse or the same, when it comes to...?"}
#' This is a wrapper function around the \code{\link{rescale_categories}}
#' function.
#' @param column A column from a survey data frame where gender is recorded.
#' @param from Defaults to \code{"Better"}, \code{"The same"},
#' \code{"Worse"}.
#' @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("better", "same", "worse"}.
#' @param na_labels  Defaults to \code{c("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_outlook (column =
#'                         c("DK",
#'                           "Better",
#'                           "Worse",
#'                           "worse",
#'                           "The same"),
#'                       underscore = FALSE,
#'                       return_class = "character")
#'
#'  rescale_outlook (column =
#'                         c("DK",
#'                           "Better",
#'                           "Worse",
#'                           "worse",
#'                           "The same"),
#'                       return_class = "numeric")
#' @export

rescale_outlook <- function ( column,
                                 from = c("Better",
                                          "Same",
                                          "Worse"),
                                 to = c(1,0,-1),
                                 na_labels = "default",
                                 exact_from = TRUE,
                                 return_class = "numeric",
                                 underscore = TRUE) {

  if (from[1] == "hungary") {
    from <- c("jobb", "rosszabb", "ugyanolyan")
    exact_from <- FALSE
    if ( return_class %in% c("character", "factor")) {
      to = c("jobb", "ugyanaz", "rosszabb")
    }
  }

  if ( return_class %in% c("character", "factor")) {
    to = c("better","same", "worse")
  }

  if ( "labelled" %in% class(column)  ) {
    column <- haven::as_factor (column) }


  column <- tolower(as.character(column))
  column <- gsub("the same", "same", column)
  from  <- tolower(from)
  from <- gsub( "the same", "same", from )
  column <- stringr::str_trim ( column, side = "both")


  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.