tidytable
tidytable copied to clipboard
Implement `case_match.()`?
Don't know if you would expect any performance improvements compared to using dplyr::recode()
inside tidytable::mutate.()
calls or if a wrapper function makes sense because of consistency anyway.
On one hand I think it makes sense to add this since it's in dplyr
. On the other hand the documentation for recode()
makes it seem like they might be replacing it moving forward:
recode() is questioning because the arguments are in the wrong order. We have new <- old, mutate(df, new = old), and rename(df, new = old) but recode(x, old = new). We don't yet know how to fix this problem, but it's likely to involve creating a new function then retiring or deprecating recode().
What do you think @marianschmidt - should it be added to tidytable anyway? I'm not sure which way I'm leaning.
Ah, sorry, forgot about this questioning status of the function. On the other hand, nothing happened in this discussion over the last two years (https://github.com/tidyverse/dplyr/issues/4851). And it is quite a useful function. For me personally, I wouldn't need it since dplyr::recode()
works inside tidytable::mutate.()
without any problems or serious performance loss.
So I think, we should not expect any soon deprecation of recode()
, so if your general approach is to provide tidytable
equivalents for all dplyr
functions, then I would say, it makes sense to implement.
As an update - I think I'm going to hold off on this until the tidyverse team implements their new version.
They seem to be working on a couple different options here.
I guess recode.() still would be great to avoid using dplyr only for recode.
dplyr's v1.1.0 release looks like it's coming soon. I think the new function should come in that. They were looking at calling it case_match()
or case_switch()
. If they don't come out with the new version in v1.1.0 I'll implement recode.()
in tidytable.
Great news, thank you!
All set.
library(tidytable, warn.conflicts = FALSE)
#> As of tidytable v0.9.0 dotless versions of functions are exported.
#> You can now use `arrange()`/`mutate()`/etc. directly.
df <- tidytable(x = c("a", "b", "c", "d"))
df %>%
mutate(
case_x = case_match(x,
c("a", "b") ~ "A",
"c" ~ "B",
.default = x)
)
#> # A tidytable: 4 × 2
#> x case_x
#> <chr> <chr>
#> 1 a A
#> 2 b A
#> 3 c B
#> 4 d d
Thought about this some more - recode()
is being superseded but not deprecated, so I'm going to add recode()
to tidytable as well.
See #663