Skip to content

Instantly share code, notes, and snippets.

@pedroj
Created March 25, 2013 16:31
Show Gist options
  • Select an option

  • Save pedroj/5238456 to your computer and use it in GitHub Desktop.

Select an option

Save pedroj/5238456 to your computer and use it in GitHub Desktop.
Reorder levels of a factor
## generate data
x = factor(sample(letters[1:5],100, replace=TRUE))
print(levels(x)) ## This will show the levels of x are "Levels: a b c d e"
## To reorder the levels:
## note, if x is not a factor use levels(factor(x))
x = factor(x,levels(x)[c(4,5,1:3)])
print(levels(x)) ## Now "Levels: d e a b c"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment