Created
March 25, 2013 16:31
-
-
Save pedroj/5238456 to your computer and use it in GitHub Desktop.
Reorder levels of a factor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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