[R] Factor levels.

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 3 22:36:27 CEST 2007


Peter Dalgaard wrote:
> Rolf Turner wrote:
>   
>>     P.S.  ***Are*** there any risks/dangers in following Christos 
>> Hatzis' suggestion of simply doing
>>
>>             levels(fff) <- c("U","A","S")       ???
>>     
> Not if the levels are right to begin with.
>
> Problems only arise if fff somehow becomes a two-level factor, e.g. if 
>   
Doh. Sorry. Mixing up the examples there:  "...arise if fff somehow 
loses levels..." the example below of course has two levels reduced to a 
one.

> you do
>    fff <- fff[2:3, drop=TRUE].
> Then you can get this effect:
>
>  >  fff
> [1] Unit        Scholarship Scholarship Unit      
> Levels: Scholarship Unit
>  > fff[2:3, drop=TRUE]
> [1] Scholarship Scholarship
> Levels: Scholarship
>  > fff[-(2:3), drop=TRUE]
> [1] Unit Unit
> Levels: Unit
>  > ggg <- fff[2:3, drop=TRUE]
>  > levels(ggg) <- c("a","b")
>  > ggg
> [1] a a
> Levels: a b
>  > ggg <- fff[-(2:3), drop=TRUE]
>  > levels(ggg) <- c("a","b")
>  > ggg
> [1] a a
> Levels: a b
>
>
>   


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