Skip to content

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

Merged
merged 13 commits into from
Jan 28, 2021
Merged

if_any() effectively reducing. #5713

merged 13 commits into from
Jan 28, 2021

Conversation

romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Jan 28, 2021

closes #5709

simplified version of @iago-pssjd example:

library(dplyr, warn.conflicts = FALSE)
d <- data.frame(x = c(0, 0, 10), y = c(0, 0, 10), z = c(10, 0, 10))
d %>% mutate(
  test = case_when(
    if_all(x:z, ~ . > 8) ~ "all big",
    if_any(x:z, ~ . > 8) ~ "some big",
    TRUE                 ~ "small"
  ))
#>    x  y  z     test
#> 1  0  0 10 some big
#> 2  0  0  0    small
#> 3 10 10 10  all big

Created on 2021-01-28 by the reprex package (v0.3.0)

Copy link
Member

@lionel- lionel- left a 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()!

@romainfrancois romainfrancois requested a review from hadley January 28, 2021 10:25
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Member Author

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

Copy link
Member Author

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)

@romainfrancois romainfrancois merged commit 1234903 into master Jan 28, 2021
@romainfrancois romainfrancois deleted the if_any_reduce branch January 28, 2021 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

On the dev feature if_any/if_all (this is not a bug)
4 participants