The result of 'tapply' is just a named vector and the names are in
alphabetical order.  If you want them printed in a different order, then you
have to specify it.  Since you have the order in 'mylevels', this will work:

> str(mysummary)
 int [1:3(1d)] 4 3 2
 - attr(*, "dimnames")=List of 1
  ..$ : chr [1:3] "IN0019800021" "IN0020020064" "IN0020020155"
> mysummary[mylevels]
IN0020020155 IN0019800021 IN0020020064
           2            4            3
>


On Wed, May 6, 2009 at 9:45 AM, Chirantan Kundu <chirantan@2pirad.com>wrote:

> Hi,
>
> Does tapply change the order when applied on a factor? Below is the code I
> tried.
>
> > mylevels<-c("IN0020020155","IN0019800021","IN0020020064")
> >
>
> mydata<-c("IN0020020155","IN0019800021","IN0020020064","IN0020020155","IN0019800021","IN0019800021","IN0020020064","IN0020020064","IN0019800021")
> > myfactor<-factor(mydata,levels=mylevels)
> > myfactor
> [1] IN0020020155 IN0019800021 IN0020020064 IN0020020155 IN0019800021
> IN0019800021 IN0020020064 IN0020020064 IN0019800021
> Levels: IN0020020155 IN0019800021 IN0020020064
> > summary(myfactor)
> IN0020020155 IN0019800021 IN0020020064
>           2            4            3
>
> # Everything fine upto this point. The order of levels is maintained as it
> is.
>
> > mysummary<-tapply(myfactor,mydata,length)
> > mysummary
> IN0019800021 IN0020020064 IN0020020155
>           4            3            2
>
> # Now the order has changed.
>
> Is this the expected behavior? Any idea on how to avoid the change in
> order?
>
> Regards,
> Chirantan
>
> ____________________________________
> Visit us at http://www.2pirad.com
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]

