I have a tibble containing events and their probabilities. The number of outcomes can be two (Yes/No) or greater (A/B/C/…). In the latter case, I have a exhaustive list of events, so I want to do nothing:
ok <- tibble(
event = c("A", "B", "C"),
prob = c(0.1, 0.5, 0.4)
)
If there are only two events, I only have one row, for the probability of the event occurring:
not_ok <- tibble(
event = "Yes",
prob = 0.4
)
These tibbles are modified in a pipe chain. At a precise point, I want to add a row “No” to tibbles of the second type.
Currently, I’m interrupting the pipe to do:
if (nrow(not_ok) == 1) {
not_ok %<>%
add_row(event = "No", prob = 1-not_ok$prob)
}
And then I resume the pipe. However doing this is slow, ugly, and requires more assignments.
Is it possible to include this conditional statement inside the pipe chain, without separately creating an if statement? The code in the end should look like:
data %>%
do something %>%
add row with "No" if necessary %>%
do something else %>%
plot