You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fct_relevel() should gain fct_recode() functionality #319
I don't think this could break any existing code, but if this is very unfortunately not achievable, I would at least expect some warning saying that fct_relevel() is not expecting a named ellipsis.
I think check_dots_unnamed() is the best option for now. At some point it seems like we should rethink the design of the forcats functions, but now doesn't feel like the time.
changed the title [-]Feature request: add `fct_recode()` functionnality to `fct_relevel()`[/-][+]`fct_relevel()` should gain `fct_recode()` functionality[/+]on Jan 3, 2023
@DanChaltiel There was a great function that did exactly what I think you (and a few others) have requested at #45 (comment). We were using it a lot until @hadley's "fix" here broke it, though I admit it did require knowing what you were doing and relied on the fact that the names were passed through unchanged so maybe the news quote is only half unfair.
I can give you this version which is working for me, although it feels a bit more brittle
library(forcats)
fct_reorg=function(fac, ...) {
fct_relevel(fct_recode(fac, ...), names(rlang::dots_list(...)))
}
x<- fct(c("apple", "bear", "apple", "bear"))
x#> [1] apple bear apple bear #> Levels: apple bear
fct_recode(x, animal="bear", fruit="apple")
#> [1] fruit animal fruit animal#> Levels: fruit animal# expected output:out_pipe=x %>%
fct_recode(animal="bear", fruit="apple") %>%
fct_relevel("animal", "fruit")
out_pipe#> [1] fruit animal fruit animal#> Levels: animal fruit# or using the functionout_reorg= fct_reorg(x, animal="bear", fruit="apple")
out_reorg#> [1] fruit animal fruit animal#> Levels: animal fruit
identical(out_pipe, out_reorg)
#> [1] TRUE# works with splicingvars2recode_and_relevel= c(animal="bear", fruit="apple")
out_splice= fct_reorg(x, !!!vars2recode_and_relevel)
out_splice#> [1] fruit animal fruit animal#> Levels: animal fruit
identical(out_pipe, out_splice)
#> [1] TRUE
Activity
hadley commentedon Jan 3, 2023
I think
check_dots_unnamed()is the best option for now. At some point it seems like we should rethink the design of the forcats functions, but now doesn't feel like the time.[-]Feature request: add `fct_recode()` functionnality to `fct_relevel()`[/-][+]`fct_relevel()` should gain `fct_recode()` functionality[/+]Check that dots are unnamed where appropriate
Check that dots are unnamed where appropriate (#333)
nstjhp commentedon May 15, 2023
@DanChaltiel There was a great function that did exactly what I think you (and a few others) have requested at #45 (comment). We were using it a lot until @hadley's "fix" here broke it, though I admit it did require knowing what you were doing and relied on the fact that the names were passed through unchanged so maybe the news quote is only half unfair.
I can give you this version which is working for me, although it feels a bit more brittle
Created on 2023-05-15 with reprex v2.0.2
I agree that there should be a forcats function that can do this properly.
DanChaltiel commentedon May 17, 2023
Thanks for the function, that will be very helpful :-)
I guess we will have to wait a bit more until it becomes part of
forcatsthough.