R/moderate_more.R

#' Moderate the value of "more" or similar
#'
#' When numeric answer options have a "more" value, it substitutes more with
#' the largest explicit number, or the largest explicit number multiplied by an
#' increase factor.  If the increase factor is less than 1.0 it can be used to moderate
#' the smallest value for "less" or similar statements among numeric answers.
#' @param x A numeric vector where "more" or other values have to be replaced.
#' @param moderate The threshold to start moderation.
#' @param increase Do you want to increase/decrease the last explicit value?
#' Defaults to \code{1.0}. If \code{max(x)=100} and set to \code{increase = 1.2} than the replacement value will be \code{120}.
#' @param rounding  Should the increased/decreased values be rounded? Defaults to rounding to
#' nearest integer value with \code{rouding = 0} digits.
#' @examples
#'
#' raw_column <- c("12", "20", "3", "more")
#' raw_column <- ifelse ( raw_column == "more",
#'                        yes = "999",
#'                        no = raw_column )
#'
#' raw_column <- as.numeric ( raw_column )
#'
#' moderate_more ( x = raw_column,
#'                 moderate = 999,
#'                 increase = 1.0,
#'                 rounding = 0 )
#'
#' moderate_more ( x = raw_column,
#'                 moderate = 999,
#'                 increase = 1.26,
#'                 rounding = 1)
#'
#' @export


moderate_more <- function (x,
                           moderate = 999,
                           increase = 1.0,
                           rounding = 0) {

  if ( !is.na(increase) ) {
    x <- ifelse (x == moderate,
                 yes = round(
                   sort(unique(x), decreasing = T)[2]*increase, rounding),
                 no = x )
  } else {
    x <- ifelse (x == moderate,
                 yes = sort(unique(x), decreasing = T)[2],
                 no = x )
  }
  return (x)
}
antaldaniel/surveyreader documentation built on May 16, 2019, 2:29 a.m.