Learn R Programming

tidytidbits (version 0.2.3)

execute_if: Conditional execution in a pipeline

Description

A verb for a magrittr pipeline: execute_if_else: The language is executed only if .predicate is true.

Usage

execute_if(.data, .predicate, .language)

execute_if_else(.data, .predicate, .language_true, .language_false)

Arguments

.data

Data argument, typical "first" argument in dplyr verbs

.predicate

Evaluated to boolean. If true, executes and returns language; otherwise, returns untouched .data

.language

Language call to execute. Write is just as if you would without the execute_if: Will be used as the right-hand side of "%>%" with all possible options of magrittr.

.language_true

Execute if predicate it TRUE

.language_false

Execute if predicate it FALSE

Value

Result of .language

Examples

Run this code
# NOT RUN {
library(magrittr)
library(dplyr)
library(tibble)
library(stringr)
convert_to_quartiles <- TRUE
tibble(score=c(1,2,3,4,1,2,3,4,2,3,2,3,4,3,3)) %>%
  mutate(do_something=1) %>%
  execute_if(convert_to_quartiles,
             mutate(score = cut(score, 4, labels = str_c("Quartile ", 1:4)))) %>%
  filter(score > 2)
# }

Run the code above in your browser using DataLab