-
Notifications
You must be signed in to change notification settings - Fork 2.1k
if_any() effectively reducing. #5713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of if_any()
!
Thanks @lionel- Co-authored-by: Lionel Henry <[email protected]>
ea88abf
to
f02bc9f
Compare
for (R_xlen_t j = 0; j < ncols; j++) { | ||
int* p_df_j = LOGICAL(p_df[j]); | ||
for (int i = 0; i < n; i++) { | ||
p_reduced[i] = p_reduced[i] == TRUE || p_df_j[i] == TRUE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p_reduced[i] = p_reduced[i] == TRUE || p_df_j[i] == TRUE; | |
p_reduced[i] = p_reduced[i] || p_df_j[i]; |
Can the comparison be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think not because of NA_LOGICAL but I have to check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if I make these two changes, I get:
library(dplyr, warn.conflicts = FALSE)
df <- expand.grid(
x = c(TRUE, FALSE, NA), y = c(TRUE, FALSE, NA)
)
filter(df, x, y)
#> x y
#> 1 TRUE TRUE
filter(df, x & y)
#> x y
#> 1 TRUE TRUE
filter(df, if_all(c(x,y), identity))
#> x y
#> 1 TRUE TRUE
#> 2 NA TRUE
#> 3 TRUE NA
#> 4 NA NA
filter(df, x | y)
#> x y
#> 1 TRUE TRUE
#> 2 FALSE TRUE
#> 3 NA TRUE
#> 4 TRUE FALSE
#> 5 TRUE NA
filter(df, if_any(c(x,y), identity))
#> x y
#> 1 TRUE TRUE
#> 2 FALSE TRUE
#> 3 NA TRUE
#> 4 TRUE FALSE
#> 5 NA FALSE
#> 6 TRUE NA
#> 7 FALSE NA
#> 8 NA NA
Created on 2021-01-28 by the reprex package (v0.3.0)
otherwise, I get:
library(dplyr, warn.conflicts = FALSE)
df <- expand.grid(
x = c(TRUE, FALSE, NA), y = c(TRUE, FALSE, NA)
)
filter(df, x, y)
#> x y
#> 1 TRUE TRUE
filter(df, x & y)
#> x y
#> 1 TRUE TRUE
filter(df, if_all(c(x,y), identity))
#> x y
#> 1 TRUE TRUE
filter(df, x | y)
#> x y
#> 1 TRUE TRUE
#> 2 FALSE TRUE
#> 3 NA TRUE
#> 4 TRUE FALSE
#> 5 TRUE NA
filter(df, if_any(c(x,y), identity))
#> x y
#> 1 TRUE TRUE
#> 2 FALSE TRUE
#> 3 NA TRUE
#> 4 TRUE FALSE
#> 5 TRUE NA
Created on 2021-01-28 by the reprex package (v0.3.0)
closes #5709
simplified version of @iago-pssjd example:
Created on 2021-01-28 by the reprex package (v0.3.0)