50% off | Unlimited Data & AI Learning
Get 50% off unlimited learning

rvec (version 0.0.7)

if_else_rvec: Vectorised If-Else, When Condition is an Rvec

Description

A version of if_else for the situation where condition is an rvec.

Usage

if_else_rvec(condition, true, false, missing = NULL, size = NULL)

Value

An rvec with the same number of draws as condition.

Arguments

condition

An object of class rvec_lgl.

true, false

Vectors (including rvecs) to use for TRUE and FALSE values of condition.

missing

Vectors to use for NA values of condition. Optional.

size

Length of output. Optional.

See Also

  • base R function ifelse() does not not work correctly if any of the inputs are rvecs.

  • dplyr function if_else works correctly if arguments true, false or missing are rvecs, but not if argument condition is an rvec.

Examples

Run this code
x <- rvec(list(c(1, 11),
               c(2, 5),
               c(22, 6)))

x > 10 ## rvec_lgl

## if_else_rvec needed when
## 'condition' is an rvec
if_else_rvec(x > 10, 10, x)

## dplyr::if_else works when
## 'true', 'false', or 'missing'
## (but not 'condition') are rvecs
library(dplyr)
if_else(c(TRUE, FALSE, TRUE), x, 100)

Run the code above in your browser using DataLab