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
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. >
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. >> >
Sverre, have you tried to put minus(-) in front of the variable by which you order the other? weidong On Sun, Jul 22, 2012 at 12:27 PM, Sverre Stausland <johnsen at fas.harvard.edu> 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? > > 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.
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
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. >