[R] Reorder in decreasing order

Sverre Stausland johnsen at fas.harvard.edu
Sun Jul 22 19:34:36 CEST 2012


By looking at your output, it didn't change the order of the levels.
(This is symptomatic for how difficult it is to change levels in R in
any automatic way).

On Sun, Jul 22, 2012 at 7:31 PM, arun <smartpink111 at yahoo.com> wrote:
> Hi,
>
> Not sure if this helps or not.
>
>  with(InsectSprays, spray[order(count)])
>  [1] C C C C C C E E C C D E C C D D E E E E C D D E D D D D D E E D E E A B C F
> [39] A A F B B F A D A B F F A A A B F F B F A B B B B A A B B F A F F F
> Levels: A B C D E F
>> with(InsectSprays, spray[rev(order(count))])
>  [1] F F F A F B B A A B B B B A F B F F B A A A F F B A D A F B B F A A F C B A
> [39] E E D E E D D D D D E D D C E E E E D D C C E D C C E E C C C C C C
> Levels: A B C D E F
> A.K.
>
>
>
>
>
>
> ----- Original Message -----
> From: Sverre Stausland <johnsen at fas.harvard.edu>
> To: arun <smartpink111 at yahoo.com>
> Cc: R help <r-help at r-project.org>
> Sent: Sunday, July 22, 2012 1:10 PM
> Subject: Re: [R] Reorder in decreasing order
>
> 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