Learn R Programming

lest

Lest provides two functions for vectorised conditional recoding of variables. case_when() enables you to vectorise multiple if and else statements (like CASE WHEN in SQL). if_else() is a stricter and more predictable version of base::ifelse() that preserves attributes (and therefore works with Dates). The functions in lest are forks of the dplyr functions of the same name. For more infos please refer to the documentation.

Why use lest?

Use this package if you like the semantics of dplyr::case_when(), but do not want to use dplyr because of the dependencies it comes with. If you already use dplyr in your project, you gain no advantage from lest. lest::case_when() and lest::if_else() behave exactly identical to the dplyr equivalents, just that they do not support tidyeval syntax (like !!!).

Dependencies

lest depends only on base R, and will never add any external dependencies.

Installation

You can install lest from GitHub with:

# install.packages("devtools")
devtools::install_github("s-fleck/lest")

Example

x <- 1:50

case_when(
  x %% 35 == 0 ~ "fizz buzz",
  x %% 5 == 0 ~ "fizz",
  x %% 7 == 0 ~ "buzz",
  TRUE ~ as.character(x)
)

case_when(
  x %% 35 == 0 ~ 35,
  x %% 5 == 0 ~ 5,
  x %% 7 == 0 ~ 7,
  TRUE ~ NA
)

Copy Link

Version

Install

install.packages('lest')

Monthly Downloads

306

Version

1.1.0

License

MIT + file LICENSE

Maintainer

Stefan Fleck

Last Published

November 29th, 2019

Functions in lest (1.1.0)

case_when

A general vectorised if
lest-package

lest: Vectorised Nested if-else Statements Similar to CASE WHEN in 'SQL'
cumall

Cumulative all and any
exceeds_tumbling_sum

Check When the Tumbling Sum of a Vector Exceeds a Threshold
if_else

Vectorised if