[R] Factor levels

Peter Alspach PAlspach at hortresearch.co.nz
Tue Aug 28 23:23:15 CEST 2007


Sebastain

Does the following work for you?

seb <- read.table(file='clipboard', header=T)
seb$c
 [1] w k r s k p l u z s j j x r d j x w q f
Levels: d f j k l p q r s u w x z
seb$c <- factor(seb$c, levels=unique(seb$c))
seb$c
 [1] w k r s k p l u z s j j x r d j x w q f
Levels: w k r s p l u z j x d q f

Peter Alspach
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sébastien
> Sent: Wednesday, 29 August 2007 9:00 a.m.
> To: Gabor Grothendieck
> Cc: R-help
> Subject: Re: [R] Factor levels
> 
> Ok, I cannot send to you one of my dataset since they are 
> confidential. 
> But I can produce a dummy "mini" dataset to illustrate my question. 
> Let's say I have a csv file with 3 columns and 20 rows which 
> content is reproduced by the following line.
> 
>  > mydata<-data.frame(a=1:20,
> b=sample(100:200,20,replace=T),c=sample(letters[1:26], 20, 
> replace = T))  > mydata
>     a   b c
> 1   1 176 w
> 2   2 141 k
> 3   3 172 r
> 4   4 182 s
> 5   5 123 k
> 6   6 153 p
> 7   7 176 l
> 8   8 170 u
> 9   9 140 z
> 10 10 194 s
> 11 11 164 j
> 12 12 100 j
> 13 13 127 x
> 14 14 137 r
> 15 15 198 d
> 16 16 173 j
> 17 17 113 x
> 18 18 144 w
> 19 19 198 q
> 20 20 122 f
> 
> If I had to read the csv file, I would use something like: 
> mydata<-data.frame(read.table(file="c:/test.csv",header=T))
> 
> Now, if you look at mydata$c, the levels are alphabetically ordered.
>  > mydata$c
>  [1] w k r s k p l u z s j j x r d j x w q f
> Levels: d f j k l p q r s u w x z
> 
> What I am trying to do is to reorder the levels as to have 
> them in the order they appear in the table, ie
> Levels: w k r s p l u z j x d q f
> 
> Again, keep in mind that my script should be used on datasets 
> which content are unknown to me. In my example, I have used 
> letters for mydata$c, but my code may have to handle factors 
> of numeric or character values (I need to transform specific 
> columns of my dataset into factors for plotting purposes). My 
> goal is to let the code scan the content of each factor of my 
> data.frame during or after the read.table step and reorder 
> their levels automatically without having to ask the user to 
> hard-code the level order.
> 
> In a way, my problem is more related to the way the factor 
> levels are ordered than to the read.table function, although 
> I guess there is a link...
> 
> Gabor Grothendieck a écrit :
> > Its not clear from your description what you want



More information about the R-help mailing list