Last active
October 5, 2017 16:49
-
-
Save romainfrancois/eeeed972d6734bcad3ec3dcf872df7ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rlang) | |
library(dplyr) | |
library(purrr) | |
mutate_when <- function(data, condition, ...){ | |
condition <- enquo(condition) | |
dots <- exprs(...) | |
expressions <- map2( dots, syms(names(dots)), ~{ | |
quo( case_when(..condition.. ~ !!.x , TRUE ~ !!.y ) ) | |
}) | |
data %>% | |
mutate( ..condition.. = !!condition ) %>% | |
mutate( !!!expressions ) %>% | |
select( -..condition..) | |
} | |
d <- data.frame( x = 1:4, y = 1:4) | |
mutate_when( d, x < 3, | |
x = -x, | |
y = -y | |
) |
I just followed Hadley's suggestion of using map2 and syms ...
purrr::imap
first appears in v0.2.3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error in unname(imap(dots, ~{ : could not find function "imap"
I guess I don't have a modernenoughverse install: