R/incdec.R

Defines functions incdec

incdec <- function(value, tense) {
  tryCatch({
    if  (is.null(value) | is.null(tense)) {
      # Check that input is not null, and raise an error if it is
      stop("Input to incdec is NULL", call. = FALSE)
    } else if (is.na(value) | is.na(tense)) {
      # Check that input is not null, and raise and error if it is
      stop("Input to incdec is NA", call. = FALSE)
    } else if (is.character(value) | !is.character(tense)) {
      stop("Input to incdec is a character", call. = FALSE)
    } else {
      # BODY --------------------------------------------------------------------
      
      value <- as.numeric(value) # converts strings into a number
      if (tense == "present") {
        if (value > 0) {
          "increasing by"
        }
        else if (value < 0) {
          "decreasing by"
        }
        else ("ERROR")
      }
      else if (tense == "past") {
        if (value > 0) {
          "increased by"
        }
        else if (value < 0) {
          "decreased by"
        }
        else ("was unchanged")
      }
      
      
      
      else if (tense == "singular") {
        
        if (value > 0) {
          
          "increase"
          
        }
        
        else if (value < 0) {
          
          "decrease"
          
        }
        
        else ("ERROR")
        
      }
      
      
      
      else if (tense == "plural") {
        
        if (value > 0) {
          
          "increases"
          
        }
        
        else if (value < 0) {
          
          "decreases"
          
        }
        
        else ("ERROR")
      }
      else {
        print("please provide a 'tense' as either present, past, singular or plural")
      }
    }
  }, warning = function(war){
    warning(war)
  }, error = function(err){
    err$message <- paste("While selecting description of change", err, sep = " ")
    stop(err)
  })
}
moj-analytical-services/las_rap_code_library documentation built on July 30, 2023, 5:54 p.m.