You can use both the OR operator |
or any()
It is the same thing when comparing &
and all()
.
As suggested, you must take into account that |
is vectorized, while any()
is not
In order to use any() the same way, you must group the data rowwise, so you can call an equivalent of any(current_row)
. This can be done with purrr::pmap
or dplyr::rowwise
:
Se the code below for a comparison of all methods:
df%>%mutate(row_OR=x|y,
row_pmap_any=pmap_lgl(select(.,c(x,y)), any))%>%
rowwise()%>%
mutate(row_rowwise_any=any(c_across(c(x,y))))
# A tibble: 12 x 7
# Rowwise:
x y allF allT row_OR row_pmap_any row_rowwise_any
<lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl>
1 TRUE TRUE FALSE TRUE TRUE TRUE TRUE
2 FALSE FALSE FALSE TRUE FALSE FALSE FALSE
3 TRUE TRUE FALSE TRUE TRUE TRUE TRUE
4 TRUE FALSE FALSE TRUE TRUE TRUE TRUE
5 FALSE TRUE FALSE TRUE TRUE TRUE TRUE
6 TRUE FALSE FALSE TRUE TRUE TRUE TRUE
7 TRUE TRUE FALSE TRUE TRUE TRUE TRUE
8 FALSE FALSE FALSE TRUE FALSE FALSE FALSE
9 TRUE TRUE FALSE TRUE TRUE TRUE TRUE
10 TRUE FALSE FALSE TRUE TRUE TRUE TRUE
11 FALSE TRUE FALSE TRUE TRUE TRUE TRUE
12 TRUE FALSE FALSE TRUE TRUE TRUE TRUE
All methods work, and I did not find much difference in performance.