[R] Reorder in decreasing order
Duncan Murdoch
murdoch.duncan at gmail.com
Mon Jul 23 01:45:59 CEST 2012
On 12-07-22 5:33 PM, arun wrote:
> Hi Duncan,
>
> That was my original suggestioin. His reply suggests that it is not that he wanted.
I didn't see your reply. Maybe you sent it privately? In any case, I
think it is up to Sverre to give an example of what he wants, since your
suggestion, Weidong's and mine all appear to do what he asked for.
Duncan Murdoch
> "
>
> 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."
>
>
> 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: Duncan Murdoch <murdoch.duncan at gmail.com>
> To: Sverre Stausland <johnsen at fas.harvard.edu>
> Cc: r-help at r-project.org
> Sent: Sunday, July 22, 2012 4:56 PM
> Subject: Re: [R] Reorder in decreasing order
>
> On 12-07-22 12:27 PM, Sverre Stausland wrote:
>> 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?
>
> Yes, as Weidong suggested:
>
>> x <- rnorm(20)
>> y <- factor(sample(letters[1:3], 20, replace=TRUE))
>> reorder(y, x, mean)
> [1] a a c c c b b a b a c c b b a a a a c a
> attr(,"scores")
> a b c
> -0.2012975 0.6117830 0.2180352
> Levels: a c b
>>
>> reorder(y, x, function(x) -mean(x))
> [1] a a c c c b b a b a c c b b a a a a c a
> attr(,"scores")
> a b c
> 0.2012975 -0.6117830 -0.2180352
> Levels: b c a
>
> Duncan Murdoch
>
> ______________________________________________
> 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