Created
October 11, 2016 20:52
-
-
Save tjmahr/36697532f07d73d93e53eb3372915967 to your computer and use it in GitHub Desktop.
only_if.R
This file contains hidden or 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("dplyr") | |
| only_if <- function(data, condition, pipeline) { | |
| if (condition) { | |
| pipeline(data) | |
| } else { | |
| data | |
| } | |
| } | |
| iris %>% | |
| arrange(Sepal.Length) %>% | |
| only_if(TRUE, . %>% filter(Species == "versicolor")) %>% | |
| tbl_df | |
| iris %>% | |
| arrange(Sepal.Length) %>% | |
| only_if(FALSE, . %>% filter(Species == "versicolor")) %>% | |
| tbl_df | |
| iris %>% | |
| arrange(Sepal.Length) %>% | |
| only_if(nrow(.) != 1, . %>% mutate(New = "new column")) %>% | |
| tbl_df | |
| iris %>% | |
| arrange(Sepal.Length) %>% | |
| only_if(nrow(.) == 1, . %>% mutate(New = "new column")) %>% | |
| tbl_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment