[R] Reorder in decreasing order

Sverre Stausland johnsen at fas.harvard.edu
Sun Jul 22 19:10:05 CEST 2012


Not quite. It still orders the values in an increasing order, you've
just changed the values here. I'm using reorder() to prepare for
plotting the values, so I can't change the values.

On Sun, Jul 22, 2012 at 6:51 PM, arun <smartpink111 at yahoo.com> wrote:
> Hi,
>
> I hope this is what you are looking for.
>
>
>  bymean1<-with(InsectSprays,reorder(spray,count,mean))
>> bymean1
>  [1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
> [39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
> attr(,"scores")
>         A         B         C         D         E         F
> 14.500000 15.333333  2.083333  4.916667  3.500000 16.666667
> ####################
>
> Levels: C E D A B F
>
> #####################
>
> bymean2<-with(InsectSprays,reorder(spray,count,function(x) -mean(x)))
>  bymean2
>  [1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
> [39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
> attr(,"scores")
>          A          B          C          D          E          F
> -14.500000 -15.333333  -2.083333  -4.916667  -3.500000 -16.666667
> ####################
> Levels: F B A D E C
> #######################
>
> A.K.
>
>
>
> ----- Original Message -----
> From: Sverre Stausland <johnsen at fas.harvard.edu>
> To: r-help at r-project.org
> Cc:
> Sent: Sunday, July 22, 2012 12:27 PM
> Subject: [R] Reorder in decreasing order
>
> reorder() is probably the best way to order the levels in a vector
> without manually specifying the order. But reorder() orders by default
> in an increasing order: "The levels are ordered such that the values
> returned by ‘FUN’ are in increasing order."
>
> Is there a way to do what reorder() does, but order the levels
> according to a _decreasing_ order of the values?
>
> Sverre
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list