Skip to content
Prev 244002 / 398506 Next

knowing the code-number of factors in a vector

Hi!

As Ben Bolker told you already, the levels are alphabetically ordered by 
default.
When you print rfactor, the last line shows you the different levels, in 
the saved order.
rfactor
[1] c c c d b a b d d a a e e b b e c e e a a b b b a b a e a a b d b b 
c a b b
[39] d c a e c d e d a a a a
Levels: a b c d e                   ## here

If you want to change the order manually, do this:
rfactor=as.factor(sample(letters[1:5], 50, replace=T))
rfactor
  [1] e e a a e a a a b a a d a b a b b c d c b e b e d d c a a e a b c 
e b c b e
[39] a e e d b e c b e d a e   ## different values because of sample()
Levels: a b c d e
rfactor2 <- factor(rfactor, levels=c("e","d","c","b","a"))
rfactor2
  [1] e e a a e a a a b a a d a b a b b c d c b e b e d d c a a e a b c 
e b c b e
[39] a e e d b e c b e d a e  ## but the values here are the same as for 
my rfactor
Levels: e d c b a                  ## note that the order here is the 
one you typed in your call to factor(, levels=)

HTH,
Ivan


Le 12/4/2010 02:04, Rainer Schuermann a ?crit :