casewhen: Multi-branch selection by condition

View source: R/primitives.R

casewhenR Documentation

Multi-branch selection by condition

Description

For each position, take the label of the first condition that is TRUE there. Conditions are a named list of equal-length logical vectors, in the same shape as collapsevalues()'s groups: the name of an element is the value used where that element is the first TRUE. Positions matching no condition get default. NA in a condition counts as not matching.

Usage

casewhen(conditions, default = NA)

Arguments

conditions

A named list of equal-length logical vectors. Each name is the value returned where its vector is the first TRUE.

default

Value for positions where no condition holds. Default NA.

Value

A vector as long as the conditions, holding matched labels and default elsewhere.

Examples

x <- c(-3, 0, 4, 25)
casewhen(list(neg = x < 0, low = x < 10), default = "high")

basetable documentation built on Sept. 18, 2026, 9:13 a.m.