tidytable icon indicating copy to clipboard operation
tidytable copied to clipboard

Implement `case_match.()`?

Open marianschmidt opened this issue 2 years ago • 6 comments

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.

marianschmidt avatar Mar 21 '22 14:03 marianschmidt

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.

markfairbanks avatar Mar 21 '22 14:03 markfairbanks

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.

marianschmidt avatar Mar 21 '22 15:03 marianschmidt

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.

markfairbanks avatar Apr 27 '22 14:04 markfairbanks

I guess recode.() still would be great to avoid using dplyr only for recode.

erdeyl avatar Jul 20 '22 12:07 erdeyl

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.

markfairbanks avatar Jul 21 '22 21:07 markfairbanks

Great news, thank you!

erdeyl avatar Jul 21 '22 21:07 erdeyl

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

markfairbanks avatar Sep 13 '22 21:09 markfairbanks

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

markfairbanks avatar Oct 20 '22 16:10 markfairbanks