relevel factors and reorder factors using tidyverse in R

Viewed 5584

I want to use the functions relevel() and reorder() in my data frame. I understand how relevel works but I do not understand why I do not see the change of the levels in my data.frame. For example imagine that I have the iris dataset

library(tidyverse)

head(iris)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa


iris$Species <- factor(iris$Species, levels = c("versicolor","setosa","virginica"), 
                       labels = c("versicolor","setosa","virginica"))

Created on 2022-04-12 by the reprex package (v2.0.1)

I can use this function to change the order of the levels or this function in dplyr :

iris %>% 
  mutate(Species=factor(Species)) %>% 
  mutate(Species=fct_relevel(Species,c("versicolor","setosa","virginica"))) %>% 
  head()
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa

Created on 2022-04-12 by the reprex package (v2.0.1) What i do not get is that while i see the change in the levels in my data set, when i call my data set i do not see the change of the order and this is essential to me. This is what i see

Species
setosa
...
versicolor
...
virginica
...

This is what i want to see

Species
versicolor
...
setosa
...
virginica
...

Any help to actually change the order with tidyverse is appreciated.

1 Answers

We need to assign back to make the changes in the original data. In addition to changing the order of levels, we may need to arrange the data if the rows order needs to be changed as well

iris <- iris %>% 
  mutate(Species=factor(Species)) %>% 
  mutate(Species=fct_relevel(Species,c("versicolor","setosa","virginica"))) %>%
 arrange(Species)

Or may use the assignment operator (%<>%) from magrittr

library(magrittr)
iris %<>% 
  mutate(Species=factor(Species)) %<>% 
  mutate(Species=fct_relevel(Species,c("versicolor","setosa","virginica")))%>%
  arrange(Species)

check the levels

levels(iris$Species)
[1] "versicolor" "setosa"     "virginica" 
Related