Search code examples
rplyrdplyr

Reorder factor levels: Specify first level only


Hi I'm trying to reorder factor levels, by only defining which factor level should be first.

library(plyr)
library(dplyr)
wrong <-iris %>%  mutate(Species = reorder(Species,desc(Species)))
levels(iris.re$Species)

I would like to use mutate to define f.e. versicolor to be the first factor level and sort the other factor levels. I know there are other options to do this, but I would like to use plyr/dplyr.

Thanks


Solution

  • try relevel:

    iris <- iris %>%  mutate(Species=relevel(Species,ref="versicolor"))