7

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 in 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, which 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 change the order with Tidyverse is appreciated.

LDT
  • 2,856
  • 2
  • 15
  • 32

1 Answers1

13

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" 
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hello akrun, thank for your answer. I really appreciate. Yes I still see the change of the levels but the order of species remain the same. I am uploading a screenshot in the question. Sorry i might be wrong – LDT Jun 03 '21 at 21:16
  • 1
    @LDT For that you need `arrange` I was thinking that you need to change only `levels` – akrun Jun 03 '21 at 21:17
  • can I arrange using a the order I like? – LDT Jun 03 '21 at 21:18