Hi, How to order the levels os factor not by alphabetic order but by mean of Y. Somethink like this: I have this alphabetic order:> levels(pH)[1] "alto" "baixo" "medio" the order by mean os yvar is:> sort(tapply(Riqueza,pH,mean))baixo medio alto 11.56667 20.00000 26.80000 How to make the levels of pH ordered by this mean to the result to see somethink like this:> levels(pH)[1] "baixo" "medio" "alto" I try this to make a function that I need to get ordered levels by this mean and after rename to make a automatic contrasts by comparing models.> levels(pH)[1] <- "baixomedio" > levels(pH)[2] <- "baixomedio" > levels(pH)[1] "baixomedio" "alto" and after this a make a new model to be compared with a model with 3 levels where "baixo" and "medio" is separated. without ordered levels these command is wrong because the levels(pH)[1] is "alto" and not "baixo". Normally I use recode from car package, but for automatization your syntax is a difficult. Thanks Ronaldo --> Prof. Ronaldo Reis J?nior| .''`. UNIMONTES/Depto. Biologia Geral/Lab. de Biologia Computacional | : :' : Campus Universit?rio Prof. Darcy Ribeiro, Vila Mauric?ia | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil | `- Fone: (38) 3229-8187 | ronaldo.reis em unimontes.br | chrysopa em gmail.com | ppgcb.unimontes.br | ICQ#: 5692561 | LinuxUser#: 205366
Try this: factor(pH,levels=names(sort(tapply(Riqueza,pH,mean)))) 2008/2/20, Ronaldo Reis Junior <chrysopa at gmail.com>:> Hi, > > How to order the levels os factor not by alphabetic order but by mean of Y. > Somethink like this: > > I have this alphabetic order: > > > levels(pH) > [1] "alto" "baixo" "medio" > > the order by mean os yvar is: > > sort(tapply(Riqueza,pH,mean)) > baixo medio alto > 11.56667 20.00000 26.80000 > > How to make the levels of pH ordered by this mean to the result to see > somethink like this: > > > levels(pH) > [1] "baixo" "medio" "alto" > > I try this to make a function that I need to get ordered levels by this mean > and after rename to make a automatic contrasts by comparing models. > > > levels(pH)[1] <- "baixomedio" > > levels(pH)[2] <- "baixomedio" > > levels(pH) > [1] "baixomedio" "alto" > > and after this a make a new model to be compared with a model with 3 levels > where "baixo" and "medio" is separated. > > without ordered levels these command is wrong because the levels(pH)[1] > is "alto" and not "baixo". > > Normally I use recode from car package, but for automatization your syntax > is > a difficult. > > Thanks > Ronaldo > -- > > Prof. Ronaldo Reis J?nior > | .''`. UNIMONTES/Depto. Biologia Geral/Lab. de Biologia Computacional > | : :' : Campus Universit?rio Prof. Darcy Ribeiro, Vila Mauric?ia > | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil > | `- Fone: (38) 3229-8187 | ronaldo.reis at unimontes.br | chrysopa at gmail.com > | ppgcb.unimontes.br | ICQ#: 5692561 | LinuxUser#: 205366 > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Wed, Feb 20, 2008 at 6:02 PM, Ronaldo Reis Junior <chrysopa at gmail.com> wrote:> Hi, > > How to order the levels os factor not by alphabetic order but by mean of Y. > Somethink like this: > > I have this alphabetic order: > > > levels(pH) > [1] "alto" "baixo" "medio" > > the order by mean os yvar is: > > sort(tapply(Riqueza,pH,mean)) > baixo medio alto > 11.56667 20.00000 26.80000 > > How to make the levels of pH ordered by this mean to the result to see > somethink like this: > > > levels(pH) > [1] "baixo" "medio" "alto"Have a look at ?reorder Hadley -- had.co.nz