Hi,
I have a forcats dependency in a package I author due to the use of fct_rev.
I would like to replace this using base code. However, the base version of reversing levels doesn't seem to work as expected in a tidyeval function??
I would like to make the reverse_levels function below work in tidyeval code without using fct_rev.
Thanks!
library(palmerpenguins)
library(tidyverse)
penguins |>
mutate(species = factor(species, levels = rev(levels(species)))) |>
pull(species) |>
levels()
#> [1] "Gentoo" "Chinstrap" "Adelie"
reverse_levels <- function(data, x) {
data |>
mutate(x = factor({{x}}, levels = rev(levels({{ x }})))) |>
pull( {{ x }}) |>
levels()
}
reverse_levels(penguins, species)
#> [1] "Adelie" "Chinstrap" "Gentoo"
Created on 2024-02-07 with reprex v2.1.0