2

Let's say I have the following tibble dataframe called data:

library(tibble)

data <- tribble(
    ~"ID", ~"some factor", ~"some other factor", 
    1L, "low", "high",
    2L, "very high", "low",
    3L, "very low", "low",
    4L, "high", "very high",
    5L, "very low", "very low"
)

I use the fct() function in forcats to convert my two factor variables accordingly:

library(dplyr)
library(forcats)

data <- data %>%
        mutate(across(starts_with("some"), fct))

Which gives me:

# A tibble: 5 × 3
     ID `some factor` `some other factor`
  <int> <fct>         <fct>              
1     1 low           high               
2     2 very high     low                
3     3 very low      low                
4     4 high          very high          
5     5 very low      very low 

However, when I call fct this way it's unclear to me how to specify the levels of this ordinal variable. The order I would like is:

order <- c("very low", "low", "high", "very high")

How should I do this with dplyr's set of functions? The goal is to have ggplot2 visualizations that respect this ordering.

zx8754
  • 52,746
  • 12
  • 114
  • 209
hpy
  • 1,989
  • 7
  • 26
  • 56

2 Answers2

3
order <- c("very low", "low", "high", "very high")

data <- data %>%
  mutate(across(starts_with("some"), fct, order))

should do the trick

Onyambu
  • 67,392
  • 3
  • 24
  • 53
3

When you use across() you can pass extra arguments along to the called function through across's ....

data <- data %>%
  mutate(across(starts_with("some"), fct, levels = order))

This is equivalent to

data <- data %>%
  mutate(across(starts_with("some"), function(x) fct(x, levels = order)))

(This is a common paradigm in R, many functions where you are applying a function have a ... argument for arguments that will be passed along to the applied function, see also lapply, sapply, purrr::map, etc.)

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you! BTW, when I ran the code it gave this warning "The '...' argument of 'across()' is deprecated as of dplyr 1.1.0", so I used your second version with an anonymous function instead. – hpy Feb 06 '23 at 18:15
  • Interesting! I must be a bit out-of-date :) – Gregor Thomas Feb 06 '23 at 18:17