[R] Question about creating unique factor labels with the factor function

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jul 20 12:16:33 CEST 2005


"Petr Pikal" <petr.pikal at precheza.cz> writes:

> Hallo
> 
> I am not sure what you want to achieve.
> 
> your factor has 3 levels but with only 2 different labels
> 
> > hb
> [1] 1 1 1 1 1 1 2 2 2
> Levels: 1 1 2
> 
> but
> 
> > str(hb)
>  Factor w/ 3 levels "1","1","2": 1 1 1 2 2 2 3 3 3
> 
> so you gave only one label to level 1 and 2. You can give the same 
> label to any level you choose but I can not see a reason in it.

Well, it does look a bit like a bug compared to the semantics of e.g.
levels<-  In particular:

> hb <- factor(c(1,1,1,2,2,2,3,3,3), levels=c(1,2,3),labels=c(1,1,2))
> hb
[1] 1 1 1 1 1 1 2 2 2
Levels: 1 1 2
> levels(hb) <- levels(hb)
> hb
[1] 1 1 1 1 1 1 2 2 2
Levels: 1 2

Conceptually, one might want to replace 

    attr(f, "levels") <-

with 

    levels(f) <- 

inside factor(). However, f does not have class "factor" at that point
and if it did, we'd have a recursive call to factor() and the thing
would blow up... 


-- 
   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