Skip to contents

# Interpret specific binary values as logicals

Source: `R/as_logical.R`
`as_logical.Rd`

Interpret specific binary values as logicals

## Usage

    as_logical(
      x,
      values = list(c("TRUE", "FALSE"), c("Yes", "No"), c(1, 0), c(1, 2)),
      ...
    )

    # S3 method for class 'data.frame'
    as_logical(
      x,
      values = list(c("TRUE", "FALSE"), c("Yes", "No"), c(1, 0), c(1, 2)),
      ...
    )

    # Default S3 method
    as_logical(
      x,
      values = list(c("TRUE", "FALSE"), c("Yes", "No"), c(1, 0), c(1, 2)),
      ...
    )

## Arguments

x

  vector or data.frame

values

  list of values to interpret as logicals. First value is

...

  ignored interpreted as TRUE.

## Value

vector

## Examples

    c(sample(c("TRUE", "FALSE"), 20, TRUE), NA) |>
      as_logical() |>
      class()
    #> [1] "logical"
    ds <- dplyr::tibble(
      B = factor(sample(c(1, 2), 20, TRUE)),
      A = factor(sample(c("TRUE", "FALSE"), 20, TRUE)),
      C = sample(c(3, 4), 20, TRUE),
      D = factor(sample(c("In", "Out"), 20, TRUE))
    )
    ds |>
      as_logical() |>
      sapply(class)
    #>         B         A         C         D
    #> "logical" "logical" "numeric"  "factor"
    ds$A |> class()
    #> [1] "factor"
    sample(c("TRUE",NA), 20, TRUE) |>
      as_logical()
    #>  [1]   NA   NA TRUE   NA   NA   NA   NA   NA TRUE TRUE TRUE   NA TRUE TRUE TRUE
    #> [16] TRUE   NA TRUE TRUE TRUE
    as_logical(0)
    #> [1] FALSE