-
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
Adjusting to different behaviour between case_match
and recode
#6856
Comments
Ok, I've figured you just need an extra step to convert the named list to a formula list and we're good. There seems to be some performance benefit too. library(dplyr, warn.conflicts = FALSE)
x <- rep(1:5, 5)
recode_list <- setNames(1:5, 5:1)
formula_list <- paste0(names(recode_list), "~" ,recode_list) |>
lapply(as.formula)
bench::mark(
recode = dplyr::recode(x, !!!recode_list),
case_match = dplyr::case_match(x, !!!formula_list)
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 recode 1.99ms 2.04ms 479. 1.1MB 14.8
#> 2 case_match 199.3µs 211.15µs 4515. 297.7KB 25.6 Created on 2023-06-20 with reprex v2.0.2 |
While the above is possible, I think we should at least have one recoding function that allows us the splice in arguments. After stuying the life cycle stages here it seems like "superseded" functions will not be deprecated in the future and basically stay "forever". If that is really the case then this issue can be closed, because there is not need to make adjustments to Here are a few suggestions:
A remaining question is: if splicing is an important feature for recoding functions, which I think it is, then is |
See #6623, Hopefully, there might be better one. |
First - My goal with needles <- rep(1:5, 5)
haystacks <- as.list(1:5)
values <- as.list(5:1)
dplyr:::vec_case_match(
needles = needles,
haystacks = haystacks,
values = values
)
#> [1] 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 I think |
Is there any plan to allow spliced arguments in |
Since
recode
is being superseded, I'm looking at changing tocase_match
. I usually use it to reverse code an integer vector (e.g. 1 -> 5, 5 -> 1, etc.) and have found named list/vector useful butcase_match
doesn't accept the same.The named list approach is useful to include
recode
in other functions as named list are more straightforward to produce.How should I adapt here?
Created on 2023-05-23 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: