[R] Ordering data.frame based on class
Henrique Dallazuanna
wwwhsd at gmail.com
Mon Mar 28 23:02:03 CEST 2011
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
More information about the R-help
mailing list