[R] How to sort a data.frame
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jul 16 06:00:00 CEST 2007
In thinking about this a bit more an even simpler solution based on ave is:
storesales <- ave(T$sales, T$store, FUN = sum)
T[order(storesales, T$sales, decreasing = TRUE), ]
On 7/15/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> The example could be simplified like this:
>
> T <- productSalesByStore
> T <- merge(T, rowsum(T$sales, T$store), by.x = 2, by.y = 0)
> T[order(T$V1, T$sales, decreasing = TRUE), 1:3]
>
> If you transfer your data to a data base then this SQL statement would
> also do it:
>
> select store, product, sales
> from productSalesByStore
> natural join (select store, sum(sales) storesales from
> productSalesByStore group by store)
> order by storesales desc, sales desc
>
> On 7/15/07, Andrew Prendergast <ap at tellusion.com> wrote:
> > I noticed a decent guide on how to sort data.frames is somewhat lacking.
> >
> > To fill the gap I have written a quick post on the subject, which is here:
> >
> > http://www.andrewprendergast.com/2007/07/sorting_a_dataframe_in_r.html
> >
> > Regards,
> >
> > ap.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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