R/to_logical.R

Defines functions to_logical

Documented in to_logical

#' @title Convert categorical representations of true/false values to a logical
#'
#' @description Allow the convertion of logical related data to actual Boolean
#'
#' @param x a vector of boolean compatible values.
#'
#' @return a vector of boolean
#'
#' @examples
#' to_logical(c("yes", "no"))
#' @export

to_logical <- function(x) {
  logical_cat <- data.frame(
    cat = c(
      "y", "yes", "t", "true", "1", "n", "no", "f", "false", "0", "on",
      "off", ""
    ),
    value = c(
      TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
      TRUE, FALSE, FALSE
    )
  )

  return(logical_cat$value[match(tolower(x), logical_cat$cat)])
}
durandmorgan/DecoupleR documentation built on Jan. 1, 2021, 12:09 a.m.