as_logical: Interpret specific binary values as logicals

View source: R/as_logical.R

as_logicalR Documentation

Interpret specific binary values as logicals

Description

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()
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)
ds$A |> class()
sample(c("TRUE",NA), 20, TRUE) |>
  as_logical()
as_logical(0)

REDCapCAST documentation built on April 4, 2025, 3:18 a.m.