Skip to content
Prev 179474 / 398513 Next

tapply changing order of factor levels?

Hi,

I meant that your problem occured because the levels of mylevels are not 
ordered whereas tapply uses the ordered levels for printing. If you 
order them (look under), you can see the results of the tapply has the 
same order as the levels of myfactor

 >mydata<-c("IN0020020155","IN0019800021","IN0020020064","IN0020020155","IN0019800021","IN0019800021","IN0020020064","IN0020020064","IN0019800021")
 > mylevels<-c("IN0020020155","IN0019800021","IN0020020064")
 > myfactor<-factor(mydata,levels=mylevels)
 > myfactor
[1] IN0020020155 IN0019800021 IN0020020064 IN0020020155 IN0019800021
[6] IN0019800021 IN0020020064 IN0020020064 IN0019800021
Levels: IN0020020155 IN0019800021 IN0020020064
 > levels(myfactor) <- sort(mylevels)
 > myfactor
[1] IN0019800021 IN0020020064 IN0020020155 IN0019800021 IN0020020064
[6] IN0020020064 IN0020020155 IN0020020155 IN0020020064
Levels: IN0019800021 IN0020020064 IN0020020155
 > tapply(myfactor,mydata,length)
IN0019800021 IN0020020064 IN0020020155
           4            3            2
Chirantan Kundu wrote: