Re: [R] Ordering data.frame based on class

From: Vincy Pyne <vincy_pyne_at_yahoo.ca>
Date: Mon, 28 Mar 2011 14:04:45 -0700 (PDT)


Dear sir,

Thanks for the great solution.

Regards

Vincy

From: Henrique Dallazuanna <wwwhsd_at_gmail.com> Subject: Re: [R] Ordering data.frame based on class To: "Vincy Pyne" <vincy_pyne_at_yahoo.ca>
Cc: r-help_at_r-project.org
Received: Monday, March 28, 2011, 9:02 PM

Try this:

my_dat[order(my_dat$class, -my_dat$var1, decreasing = TRUE),]

On Mon, Mar 28, 2011 at 5:55 PM, Vincy Pyne <vincy_pyne_at_yahoo.ca> wrote:
> Dear R helpers
>
> Suppose I have a data.frame as given below -
>
> my_dat = data.frame(class = c("XYZ", "XYZ", "XYZ", "XYZ", "XYZ","ABC", "ABC", "ABC", "ABC", "ABC" ),  var1 = c(20, 14, 89, 81, 17, 44, 36, 41, 11, 36), var2 = c(1001, 250, 456, 740, 380, 641, 111, 209, 830, 920))
>
>> my_dat
>    class var1 var2
> 1    XYZ   20 1001
> 2    XYZ   14  250
> 3    XYZ   89  456
> 4    XYZ   81  740
> 5    XYZ   17  380
> 6    ABC   44  641
> 7    ABC   36  111
> 8    ABC   41  209
> 9    ABC   11
>  830
> 10   ABC  20  920
>
> I wish to sort above data.frame class-wise on var1. Thus, Ineed to get
>
>
> class                    var1        var2
>
>
>
>  XYZ
>  14
>  250
>
>
>  XYZ
>  17
>  380
>
>
>  XYZ
>  20
>  1001
>
>
>  XYZ
>  81
>  740
>
>
>  XYZ
>  89
>  456
>
>
>  ABC
>  11
>  830
>
>
>  ABC
>  20
>  920
>
>
>  ABC
>  36
>  111
>
>
>  ABC
>  41
>  209
>
>
>  ABC
>  44
>  641
>
>
> Kindly guide
>
> Vincy
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



	[[alternative HTML version deleted]]


______________________________________________ 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.

Received on Mon 28 Mar 2011 - 21:07:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Mar 2011 - 22:10:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive