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

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Wed 20 Jul 2005 - 20:16:33 EST

"Petr Pikal" <petr.pikal@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@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
```
Received on Wed Jul 20 20:45:51 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:51 EST