when: Evaluate When

Description Usage Arguments Value Examples

Description

Evaluate conditional expressions and if it evaluates to TRUE, execute the function call's logic. Note that these functions are subtly different from the scoped variants of dplyr functions in that they can evaluate any condition. If the condition evaluates to FALSE, these functions will return the original data.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
arrange_when(.data, .cond, ...)

distinct_when(.data, .cond, ...)

filter_when(.data, .cond, ...)

group_by_when(.data, .cond, ...)

mutate_when(.data, .cond, ...)

select_when(.data, .cond, ...)

summarise_when(.data, .cond, ...)

transmute_when(.data, .cond, ...)

Arguments

.data

A tbl_spark or a data.frame.

.cond

A condition that will evaluate to a logical(1). When it evaluates to TRUE, arguments passed to ... will be evaluated in the context of .data.

...

Additional parameters to pass to the dplyr function.

Value

A tbl_spark or a data.frame depending on the input, .data.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Let's say we have another object and based on this object we
# want to perform some conditional logic
previous_result <- 42

# We can evaluate expressions in the same way as the dplyr function.
# If the evaluation is FALSE, it will return the original data.
mtcars %>%
  mutate_when(previous_result < 42, mpg * 2)
# And if the condition if TRUE, it will evaluate
mtcars %>%
  mutate_when(previous_result >= 42, mpg * 2)

# We can evaluate multiple expressions
mtcars %>%
  mutate_when(previous_result >= 42, mpg2 = mpg * 2, mpg4 = mpg * 2)

# We can still use functionality such as tidy-select
mtcars %>%
  select_when(previous_result >= 42, Cylinders = cyl, everything())

# We can also evaluate logic that is conditional on .data
mtcars %>%
  filter_when("mpg" %in% colnames(.), cyl == 4)

nathaneastwood/sparkplugs documentation built on Feb. 28, 2021, 4:57 p.m.