-
Notifications
You must be signed in to change notification settings - Fork 58
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
Case When/Fcase Error #300
Comments
The root problem is that you've used I'll leave this issue open because we'll need to think about a better error message, probably both here and in dplyr. |
This is because in CRAN dtplyr, calling This issue was fixed in #207 Although it's not best practice to mix |
Good to know |
Here's a smaller reprex for the issue that seems to be causing the error in the above reprex. But yeah, there could be other issues with mixing library(dtplyr, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
gb_after <-
data.frame(a = 1:2) %>%
lazy_dt() %>%
group_by(a) %>%
mutate(b = case_when(a == 2 && a != 1 ~ 'row 2', a < 2 ~ 'not row 2'))
gb_before <-
data.frame(a = 1:2) %>%
group_by(a) %>%
lazy_dt() %>%
mutate(b = case_when(a == 2 && a != 1 ~ 'row 2', a < 2 ~ 'not row 2'))
# group_by after works fine
gb_after
#> Source: local data table [2 x 2]
#> Groups: a
#> Call: copy(`_DT1`)[, `:=`(b = fcase(a == 2 && a != 1, "row 2", a <
#> 2, "not row 2")), by = .(a)]
#>
#> a b
#> <int> <chr>
#> 1 1 not row 2
#> 2 2 row 2
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
# with group_by before the summarise is on the whole data
show_query(gb_before)
#> copy(`_DT2`)[, `:=`(b = fcase(a == 2 && a != 1, "row 2", a <
#> 2, "not row 2"))]
# and cannot produce output
print(gb_before)
#> Error in fcase(a == 2 && a != 1, "row 2", a < 2, "not row 2"): Argument #3 has a different length than argument #1. Please make sure all logical conditions have the same length. Created on 2021-09-10 by the reprex package (v2.0.1) |
Can confirm switching to |
I've encountered a version of this issue that doesn't involve library(dplyr, warn.conflicts = FALSE)
library(dtplyr, warn.conflicts = FALSE)
options(dplyr.summarise.inform = FALSE)
loans <- tibble(
borrower_id = c(1,1,1,1,2,2),
loan_id = c("A", "A", "B", "B", "C", "C"),
year = c(2020, 2021, 2020, 2021, 2020, 2021),
repayments = c(0, 0, 0, 200, 150, 50)
)
#In dplyr (works)
loans %>%
group_by(borrower_id, year) %>%
summarise(
status = case_when(any(repayments > 0) ~ "Made repayments",
TRUE ~ "Did not make any repayments")
) %>%
ungroup()
#> # A tibble: 4 x 3
#> borrower_id year status
#> <dbl> <dbl> <chr>
#> 1 1 2020 Did not make any repayments
#> 2 1 2021 Made repayments
#> 3 2 2020 Made repayments
#> 4 2 2021 Made repayments
#In dtplyr (does not work)
loans %>%
lazy_dt() %>%
group_by(borrower_id, year) %>%
summarise(
status = case_when(any(repayments > 0) ~ "Made repayments",
TRUE ~ "Did not make any repayments")
) %>%
ungroup() %>%
as_tibble()
#> Error in fcase(any(repayments > 0), "Made repayments", rep(TRUE, .N), : Argument #3 has a different length than argument #1. Please make sure all logical conditions have the same length.
#In dtplyr with different grouping that includes only one row per group (works)
loans %>%
lazy_dt() %>%
group_by(loan_id, year) %>%
summarise(
status = case_when(any(repayments > 0) ~ "Made repayments",
TRUE ~ "Did not make any repayments")
) %>%
ungroup() %>%
as_tibble()
#> # A tibble: 6 x 3
#> loan_id year status
#> <chr> <dbl> <chr>
#> 1 A 2020 Did not make any repayments
#> 2 A 2021 Did not make any repayments
#> 3 B 2020 Did not make any repayments
#> 4 B 2021 Made repayments
#> 5 C 2020 Made repayments
#> 6 C 2021 Made repayments Created on 2022-02-11 by the reprex package (v2.0.1) |
@KesterJ Since dtplyr handles |
Actually I should not have closed this issue. There is still an inconsistency with dplyr, aside from the special case of an explicit library(dplyr, warn.conflicts = FALSE)
library(dtplyr)
df <- tibble(x = 1:2)
df %>%
mutate(y = case_when(x > 1 ~ 'a', any(x > 1) ~ 'b'))
#> # A tibble: 2 × 2
#> x y
#> <int> <chr>
#> 1 1 b
#> 2 2 a
lazy_dt(df) %>%
mutate(y = case_when(x > 1 ~ 'a', any(x > 1) ~ 'b'))
#> Error in fcase(x > 1, "a", any(x > 1), "b"): Argument #3 has a different length than argument #1. Please make sure all logical conditions have the same length. Created on 2022-02-11 by the reprex package (v2.0.1) |
@eutwt should this be closed? I don't see an easy workaround for this - it looks more like a |
I agree |
Hi there,
I've run into a bug while using
dtplyr
that doesn't occur while usingdplyr
and it's been difficult to debug/understand why it's happening, so dropping a reprex for y'all here! This issue felt like the most relevant but really was unable to explain what was going on and after playing with it for a while, just couldn't get it working indtplyr
.Created on 2021-09-10 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: