Take the 2-minute tour ×

I'd like to sort a categorical variable my own way. I have grouped my dataset into categories like "1-5","6-10","11-20" .... ">251" and so forth. If plot the variables or display them in a table the sequence of the legend respectively the sequence in the label is "messed up".

This is not surprising since R does not know that these unordered variables are ordered in fact. Is there a way to attach a manually defined sequence to them?

thx for any suggestions in advance!

share|improve this question
Please can you provide some sample (minimal, reproducible) code, so that we can see exactly what you are trying to do. –  Richie Cotton Jul 20 '10 at 12:20
I agree, the answer to your question was already given when you phrased it a little differently earlier. The answer is still the same, cut(). –  John Jul 20 '10 at 13:45
I don't think cut() is the answer to the more general question of reordering factors. My 2c below. –  Eduardo Leoni Jul 20 '10 at 18:13
add comment

3 Answers

up vote 4 down vote accepted

Categorical variables are stored as (or converted to be) factors when you plot them. The order they appear in the plot depends upon the levels of the factor.

You likely want to use cut to create your groups. e.g.

dfr <- data.frame(x = runif(100, 1, 256))
dfr$groups <- cut(dfr$x, seq(1, 256, 5))

This problem is also very similar to another recent SO question.

share|improve this answer
In fact, this advice seems to be just what rcs told you for your previous question stackoverflow.com/questions/3288361/… –  Richie Cotton Jul 20 '10 at 12:26
yup, you´re right. obviously I need to take a closer look to that cut function. Just used ifelse so far... thx for pointing it out. Besides, I am still interested in re-arranging the order once I set it the wrong way... –  Matt Bannert Jul 20 '10 at 12:58
cut() will allow you to set order. If you need further control the factor() allows you to set order as well. –  John Jul 20 '10 at 13:47
add comment

When I want to specify a different order for a factor manually (tedious, but sometimes necessary) here is what I do:

> ## a factor
> x <- factor(letters[1:3])
> ## write out levels with dput
> dput(levels(x))
> ## copy, paste, modify and use factor again. e.g.
> x <- factor(x, levels=c("b", "a", "c"))
> x
[1] a b c
Levels: b a c
share|improve this answer
Interesting use of dput. –  Richie Cotton Jul 23 '10 at 16:18
add comment

I like using split for that sort of thing.

vect = runif(10)

vect.categories = c(rep(LETTERS[1],5),rep(LETTERS[2],3),rep(LETTERS[5],2))

category.list =split(vect,vect.categories)


May not be related, but thought I'd offer the suggestion.

share|improve this answer
add comment

Your Answer


