update_by: update columns conditionally

View source: R/dplyr_extensions.R

update_byR Documentation

update columns conditionally

Description

update columns conditionally

Usage

update_by(D, by, ...)

Arguments

D

data frame

...

expressions

filter

predicate function (like dplyr::filter)

Value

data frame

Applies mutations to the filtered group, only.

Author(s)

Martin Schmettow

Examples

D <- tribble(~group, ~value,  1, 4, 1, 9, 2, -4, 2, -9)

D %>% mutate(value = if_else(group == 1, sqrt(value), value))
## Produces NaNs, because sqrt() is evaluated before selection

D %>% mutate_by(group == 1, value = sqrt(value))
## sqrt() is only evaluated


schmettow/bayr documentation built on March 23, 2024, 7:49 p.m.