Apart from the more common purrr::case_when
that elegantly performs a vectorised if-else statement, there is less known variant purrr::when
which is non-vectorised yet still useful in many scenarios. Like purrr::case_when
, the purrr::when
syntax also contains condition-action pairs.
However, purrr::when
doesn't seem to evaluate the first condition.
See here several cases with the example data from purrr::case_when
.
x <- 35
purrr::when(
x %% 35 == 0 ~ "fizz buzz",
x %% 5 == 0 ~ "fizz",
x %% 7 == 0 ~ "buzz",
TRUE ~ "no buzz"
)
# should give "fizz buzz" but gives
"fizz"
purrr::when(
x %% 5 == 0 ~ "fizz",
x %% 7 == 0 ~ "buzz",
TRUE ~ "no buzz"
)
# should give "fizz" but gives
"buzz"
purrr::when(
x %% 7 == 0 ~ "buzz",
TRUE ~ "no buzz"
)
# should give "buzz" but gives
"no buzz"
Side notes:
- All conditions evaluate to TRUE.
- The same behavior happens when TRUE is omitted in the last condition.
- The same behavior happens when x is a vector (e.g.
x <- 1:100
).
Is this a bug??
It could be a bug because having only one condition throws this error message:
purrr::when(
x %% 7 == 0 ~ "buzz"
)
Error: At least one matching condition is needed.
# However, case_when behaves as expected
dplyr::case_when(
x %% 7 == 0 ~ "buzz"
)
"buzz"
Any ideas?
Ok this is the answer - thanks to jennybc on github
purrr::when
requires the value as first argument!I'm leaving the question and this answer here in case others might also oversee this, assuming the same arguments as for
dplyr::case_when
.