-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Suprising behaviour of if_any() inside case_when #5782
Comments
Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you! If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down the page. Please make sure your reprex is created with the reprex package as it gives nicely formatted output and avoids a number of common pitfalls. |
@hadley Here a reprex: library(dplyr)
iris %>%
head() %>%
mutate(G.Width = case_when(if_any(ends_with("Width"), ~ . > 3.5) ~ "Big",
if_any(ends_with("Width"), ~ . > 3.1) ~ "Medium",
TRUE ~ "Small"))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species G.Width
#> 1 5.1 3.5 1.4 0.2 setosa Small
#> 2 4.9 3.0 1.4 0.2 setosa Small
#> 3 4.7 3.2 1.3 0.2 setosa Small
#> 4 4.6 3.1 1.5 0.2 setosa Small
#> 5 5.0 3.6 1.4 0.2 setosa Big
#> 6 5.4 3.9 1.7 0.4 setosa Big Created on 2021-03-03 by the reprex package (v1.0.0) |
Can you please make a smaller reprex? Is it necessary to use 150 rows to illustrate the problem? |
@hadley Reprex updated |
Simpler reprex: library(dplyr, warn.conflicts = FALSE)
df <- data.frame(
x = 1:3
)
df %>% mutate(
y1 = case_when(
if_any(everything(), ~ . >= 3) ~ "big",
if_any(everything(), ~ . >= 2) ~ "medium",
TRUE ~ "small"
),
y2 = case_when(
x >= 3 ~ "big",
x >= 2 ~ "medium",
TRUE ~ "small"
)
)
#> x y1 y2
#> 1 1 small small
#> 2 2 small medium
#> 3 3 big big Created on 2021-03-03 by the reprex package (v1.0.0) |
Even simpler reprex: library(dplyr, warn.conflicts = FALSE)
df <- data.frame(
x = 1:3
)
df %>% mutate(
y3 = if_any(x, ~ . >= 3),
y2 = if_any(x, ~ . >= 2),
data.frame(
z3 = if_any(x, ~ . >= 3),
z2 = if_any(x, ~ . >= 2)
)
) %>%
select(y2, z2)
#> y2 z2
#> 1 FALSE FALSE
#> 2 TRUE FALSE
#> 3 TRUE TRUE Created on 2021-03-03 by the reprex package (v1.0.0) |
It has to do with the library(dplyr, warn.conflicts = FALSE)
df <- data.frame(
x = 1:3
)
trace(across, tracer = quote({ print(key); print(setup$fns) }), at = 4)
#> Tracing function "across" in package "dplyr"
#> [1] "across"
df %>% mutate(
y1 = case_when(
if_any(everything(), ~ . >= 3) ~ "big",
if_any(everything(), ~ . >= 2) ~ "medium",
TRUE ~ "small"
)
)
#> Tracing across({ .... step 4
#> [1] "across({"
#> $`1`
#> <lambda>
#> function (..., .x = ..1, .y = ..2, . = ..1)
#> . >= 3
#> <environment: 0x7fbbf89bc2e8>
#> attr(,"class")
#> [1] "rlang_lambda_function" "function"
#>
#> Tracing across({ .... step 4
#> [1] "across({"
#> $`1`
#> <lambda>
#> function (..., .x = ..1, .y = ..2, . = ..1)
#> . >= 3
#> <environment: 0x7fbbf89bc2e8>
#> attr(,"class")
#> [1] "rlang_lambda_function" "function"
#> x y1
#> 1 1 small
#> 2 2 small
#> 3 3 big Created on 2021-03-04 by the reprex package (v0.3.0)
if_any <- function(.cols = everything(), .fns = NULL, ..., .names = NULL) {
df <- across({{ .cols }}, .fns = .fns, ..., .names = .names)
n <- nrow(df)
df <- vec_cast_common(!!!df, .to = logical())
.Call(dplyr_reduce_lgl_or, df, n)
} So the I'll fix library(dplyr, warn.conflicts = FALSE)
times <- function(cols, multiplier = 1) {
across({{ cols }}, function(x) {
print(multiplier)
x * multiplier
})
}
data.frame(x = 1) %>%
summarise(
times(x, 1)$x + times(x, 2)$x
)
#> [1] 1
#> [1] 1 # <-------- should be 2
#> times(x, 1)$x + times(x, 2)$x
#> 1 2 Created on 2021-03-04 by the reprex package (v0.3.0) |
I am not sure if I am doing something wrong, but If that is the case I cannot see what in next reproducible example:
Thank you!
The text was updated successfully, but these errors were encountered: