-
Notifications
You must be signed in to change notification settings - Fork 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
Consider reverting fcase recycled conditions #6352
Comments
In particular if you look back at #4258, I think every case shown there is solved by the more explicit / easier to understand |
I don't use fcase very often, probably because it did not recycle, so I found it inconvenient. |
@tdhock can you give a convincing argument for recycling the conditions? |
I use it daily. The |
actually I probably don't understand the issue, can you please clarify what "recycling conditions" and "vectorized default" mean? perhaps with an example that shows what your proposed work-around would be after reverting? there were 14 thumbs up #4258 (comment) the inconvenience that I meant is shown below: > library(data.table)
data.table 1.15.4 using 3 threads (see ?getDTthreads). Latest news: r-datatable.com
> data.table(iris)[, mycol := fcase(Petal.Width>2, paste(Species), Petal.Width<1, "small", default="medium")][]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species mycol
<num> <num> <num> <num> <fctr> <char>
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 small
---
146: 6.7 3.0 5.2 2.3 virginica virginica
147: 6.3 2.5 5.0 1.9 virginica medium
148: 6.5 3.0 5.2 2.0 virginica medium
149: 6.2 3.4 5.4 2.3 virginica virginica
150: 5.9 3.0 5.1 1.8 virginica medium
> data.table(iris)[, mycol := fcase(Petal.Width>2, "big", Petal.Width<1, "small", default=Species)][]
Error in fcase(Petal.Width > 2, "big", Petal.Width < 1, "small", default = Species) :
Length of 'default' must be 1. above/CRAN I thought it was strange that fcase allowed vectors in ... but not default (the error). > library(data.table)
data.table 1.15.99 IN DEVELOPMENT built 2024-08-07 15:42:29 UTC using 3 threads (see ?getDTthreads). Latest news: r-datatable.com
> data.table(iris)[, mycol := fcase(Petal.Width>2, paste(Species), Petal.Width<1, "small", default="medium")][]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species mycol
<num> <num> <num> <num> <fctr> <char>
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 small
---
146: 6.7 3.0 5.2 2.3 virginica virginica
147: 6.3 2.5 5.0 1.9 virginica medium
148: 6.5 3.0 5.2 2.0 virginica medium
149: 6.2 3.4 5.4 2.3 virginica virginica
150: 5.9 3.0 5.1 1.8 virginica medium
> data.table(iris)[, mycol := fcase(Petal.Width>2, "big", Petal.Width<1, "small", default=paste(Species))][]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species mycol
<num> <num> <num> <num> <fctr> <char>
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 small
---
146: 6.7 3.0 5.2 2.3 virginica big
147: 6.3 2.5 5.0 1.9 virginica virginica
148: 6.5 3.0 5.2 2.0 virginica virginica
149: 6.2 3.4 5.4 2.3 virginica big
150: 5.9 3.0 5.1 1.8 virginica virginica |
@tdhock this behavior is what I'm referring to with the default (the last example). Not sure what recycling conditions means outside of this for the default. |
I think we are all just misunderstanding each other a bit, this should clear things up # I think this is bad, and should be an error. Do not recycle `TRUE` to be the same size as the other `condition`s.
# i.e. do not copy the dplyr `case_when()` behavior of `TRUE ~`.
fcase(
iris$Sepal.Length > 5, ">5",
iris$Sepal.Length < 4, "<4",
TRUE, as.character(iris$Sepal.Length)
)
# This is fantastic, yay for vectorized `default`
fcase(
iris$Sepal.Length > 5, ">5",
iris$Sepal.Length < 4, "<4",
default = as.character(iris$Sepal.Length)
) IIUC, the PR that updated |
the goal of the original FR AIUI is to get closer to SQL's behavior in CASE WHEN statements using ELSE, which allow full column expressions in that last clause where previously we only allowed length(default)==1. |
That makes sense about the original intent but the new functionality is definitely an upgrade. If I'm understanding correctly, we have |
Going through the examples in #4258
As noted, all of those work just fine with vectorized So it's clear AFAICT that we don't need to support |
@DavisVaughan offered a suggestion that we reconsider recycling conditions in
fcase()
: vectorized default should be enough to solve the user issue:https://fosstodon.org/@davis/112909151150543355
Earmarking this to think it through more carefully & for others' input.
The text was updated successfully, but these errors were encountered: