Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Created October 4, 2017 17:05
Show Gist options
  • Save romainfrancois/79078670205f54ea56be4074cb26cdb2 to your computer and use it in GitHub Desktop.
Save romainfrancois/79078670205f54ea56be4074cb26cdb2 to your computer and use it in GitHub Desktop.
library(rlang)
library(dplyr)
library(purrr)
replace_when <- function(data, target, ...){
target <- ensym(target)
mutate( data, !!target := case_when( !!!quos(...), TRUE ~ !!target ) )
}
d <- data.frame(x = 1:10, y = 1:10)
d %>% replace_when(y,
x < 3 ~ 2L,
x > 7 ~ 8L
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment