[R] Factor levels.

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 3 21:50:24 CEST 2007

Rolf Turner wrote:
>> Does it even work? (What if it is the first or the 2nd level that is 
>> absent?
>     Yes it works.  What's the problem?
>     To beat it to death:  if the second level of fff is absent then 
> fff will consist entirely of 1's and 3's,
>     and so c("U","A","S")[fff] will consist entirely of U's and S's.  
> I can then set the levels to be
>     c("U","A","S") and get what I want.
You didn't say that fff was numeric.

If fff is a factor, then we have the problem:

 > attach(read.table(stdin(),header=T))
0: fff
1: Unit
2: Scholarship
3: Scholarship
4: Unit
 > c("U","A","S")[fff]
[1] "A" "U" "U" "A"

Actually we have another problem too, namely sort order....

Anyway, sort the labels, and

 > c("A","S", "U")[fff]
[1] "S" "A" "A" "S"

If you have the data as numeric codes, then of course

factor(fff, levels=1:3, labels=c("U","A","S"))

does the trick too.

   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

More information about the R-help mailing list