Skip to content
Prev 131155 / 398506 Next

Ordering the levels of a vector

On 12/3/07, Judith Flores <juryef at yahoo.com> wrote:
Well, you need some sort of rule that can be used to determine the
order of the levels. The default (see ?factor) is 'levels =
sort(unique(x))'. If you instead want, say, levels in the order of
first appearance (assuming that's what you mean by "original order"),
you could define

my.factor = function(x) { factor(x, levels = unique(x)) }

and then use
[1] Day -1 Day 6  Day 10
Levels: Day -1 Day 6 Day 10
-Deepayan