rfactorslevelsforcats

How can I use fct_relabel but maintain original factor levels? Or how can I set factor levels in R with partial string matches?


I'm trying to use fct_relabel() from forcats to relabel my factor to include the factor name and the number of rows situated with it, but when I use the function, it completely changes the levels that I set to the order the columns appear. How can I override this? My original names are consistent (e.g., the animal names), but the counts will vary. As a result, I can't rely on manually releveling by the new labels name, unless I can somehow do a partial match (e.g., the label name doesn't fully match but contains cat).

Any help would be much appreciated!

library(dplyr)
library(forcats)

test <- tibble(animal = factor(c("cat", "dog", "pig"), levels = c("dog", "pig", "cat")),
               n = c(100, 10, 5),
               animal_num = fct_relabel(animal, ~paste(animal, "N =", n)))

levels(test$animal) #original, correct levels
levels(test$animal_num) #levels should be "dog N = 10", "pig N = 5", "cat N = 100"

I'm also not married to having to use fct_relabel, but preference will be given to a tidyverse solution. Thank you!

Update with example that has repeating values

test <- tibble(animal = factor(c("cat", "dog", "pig", "cat", "dog"),
                               levels = c("dog", "pig", "cat")),
               n = c(100, 10, 5, 100, 10),
               animal_num = fct_relabel(animal, ~ paste(animal[order(animal)],
                                                        "N=", n[order(animal)])))

Solution

  • We can use the levels

    test <- tibble(animal = factor(c("cat", "dog", "pig"),
                 levels = c("dog", "pig", "cat")),
                   n = c(100, 10, 5),
           animal_num = fct_relabel(animal, 
         ~ paste(levels(animal), "N=", n[match(levels(animal), animal)])))
    

    -output

    > levels(test$animal)
    [1] "dog" "pig" "cat"
    > levels(test$animal_num)
    [1] "dog N= 10"  "pig N= 5"   "cat N= 100"