[R] Sorting a data frame
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Aug 3 22:31:02 CEST 2003
On Thu, 31 Jul 2003, BORGULYA Gábor wrote:
> I think this is an important example! Is there a way to make it included
> in the help of order?
Please explain why it adds to what is already there, which looks very
similar to me.
> Maybe a shortened version:
>
> # sorting a data frame
> df <- data.frame(V1 = c("W","A", "A", "B", ""), V2 = c("E", "M", "B",
> "O", "Q"))
> sorted <- df[order(df$V1, df$V2), ]
>
>
> And a question: what to do to have the sorted data frame with the row
> labels 1:n?
Re-assign the row names?
> Gábor
>
> ----------------------------------------------------------------
> Marc Schwartz írta:
> > On Wed, 2003-07-16 at 08:42, Wayne Jones wrote:
> >>Does anyone know if it is possible to sort a dataframe?
>
> > Example:
>
> # Create two column df
> df <- data.frame(V1 = c("W","A", "A", "B", ""), V2 = c("E", "M", "B",
> "O", "Q"))
> # show df unsorted
> df
>
> V1 V2
> 1 W E
> 2 A M
> 3 A B
> 4 B O
> 5 Q
>
> # now sort df, by V1, then V2
> df[order(df$V1, df$V2), ]
>
> V1 V2
> 5 Q
> 3 A B
> 2 A M
> 4 B O
> 1 W E
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list