R/convertSNtoLogical.R

Defines functions convertSNtoLogical

Documented in convertSNtoLogical

#' Convert logical varible in spanish (S/N) to logical TRUE/FALSE.
#' @param x vector to convert. Only admit "S", "N" or empty values. Empty values
#' are assumed as FALSE.
#' @return vector var converted as logical. In case a value is distinct of "S",
#' "N" or empty value, an error is thrown.
#' @export

convertSNtoLogical <- function(x){

  x <- as.character(x)

  # the regex expression "^\b|S|N$" means any empty value, or 'S' or 'N'.
  if (!all(grepl("^\\b|S|N$" , x))){
    stop("The variable only can contain 'S', 'N' or empty values.")
  }

  x[which(x == "S")] <- TRUE
  x[which(x == "N")] <- FALSE

  x <- as.logical(x)

  return(x)

}
Eucrow/sapmuebase documentation built on Jan. 31, 2024, 5:07 a.m.