Package: forcats


Function: fct_relevel()


1. Relevel the factor s_grade to have the following level order: 2, 3, K, 1 (for demonstration purposes)

Review the data (d2)

# A tibble: 4 x 3
  s_id  s_gender s_grade
  <chr> <chr>    <chr>  
1 a     m        K      
2 b     f        1      
3 c     n        3      
4 f     m        2      

Review the structure of s_grade

str(d2$s_grade)
 Factor w/ 4 levels "K","1","3","2": 1 2 3 4

Relevel s_grade

d2 <- d2 %>% 
  mutate(s_grade = fct_relevel(s_grade, "2", "3", "K", "1"))

str(d2$s_grade)
 Factor w/ 4 levels "2","3","K","1": 3 4 2 1

2. Relevel the factor s_grade to be 1,2,3,K

Review the data (d2)

# A tibble: 4 x 3
  s_id  s_gender s_grade
  <chr> <chr>    <chr>  
1 a     m        K      
2 b     f        1      
3 c     n        3      
4 f     m        2      

Review the structure of s_grade

str(d2$s_grade)
 Factor w/ 4 levels "K","1","3","2": 1 2 3 4

Relevel “K” after the 3rd position. Since the 4th position is last, you could also use the argument after = Inf to refer to the last position.

d2 <- d2 %>% 
  mutate(s_grade = fct_relevel(s_grade, "K", after = 3))

str(d2$s_grade)
 Factor w/ 4 levels "1","3","2","K": 4 1 2 3

3. Relevel the factor s_grade to be 1,K,2,3

Review the data (d2)

# A tibble: 4 x 3
  s_id  s_gender s_grade
  <chr> <chr>    <chr>  
1 a     m        K      
2 b     f        1      
3 c     n        3      
4 f     m        2      

Review the structure of s_grade

str(d2$s_grade)
 Factor w/ 4 levels "K","1","3","2": 1 2 3 4

Relevel “1” to be first. Since I only want “1” to be first and to keep the order the same otherwise, I can just say to relevel “1” and the rest will be implied.

d2 <- d2 %>% 
  mutate(s_grade = fct_relevel(s_grade, "1"))

str(d2$s_grade)
 Factor w/ 4 levels "1","K","3","2": 2 1 3 4

Package: labelled


Function: to_factor()


In the rare occasion where you have a labelled variable that you want to convert to a factor and then order those levels, you can use labelled::to_factor().

1. Order s_grade levels: K,1,2,3

Review the data (d3)

# A tibble: 4 x 3
  s_id  s_gender s_grade
  <chr> <chr>      <dbl>
1 a     m              0
2 b     f              1
3 c     n              3
4 f     m              2

Review the structure of s_grade

utils::str(d3$s_grade)
 dbl+lbl [1:4] 0, 1, 3, 2
 @ labels: Named num [1:4] 1 2 3 0
  ..- attr(*, "names")= chr [1:4] "First" "Second" "Third" "Kindergarten"

Convert s_grade to a factor and then order those levels according to their original values using the argument sort_levels = “v”.

d3v <- d3 %>% 
  dplyr::mutate(s_grade = labelled::to_factor(s_grade, sort_levels = "v"))

base::levels(d3v$s_grade)
[1] "Kindergarten" "First"        "Second"       "Third"       

If you want them to be ordered according to labels you can change the argument to sort_levels = “l”.

d3l <- d3 %>% 
  dplyr::mutate(s_grade = labelled::to_factor(s_grade, sort_levels = "l"))

base::levels(d3l$s_grade)
[1] "First"        "Kindergarten" "Second"       "Third"       

If you want them to be ordered according the order you originally assigned labels you can change the argument to sort_levels = “n”. This is also the default when you do not add the “sort_levels” option.

d3n <- d3 %>% 
  dplyr::mutate(s_grade = labelled::to_factor(s_grade, sort_levels = "n"))

base::levels(d3n$s_grade)
[1] "First"        "Second"       "Third"        "Kindergarten"

Return to Reorder