Hi there R-Helpers, Does anyone know if it is possible to sort a dataframe? I.e. Sort alphabetically column 1 ( which has some reocurring elements) then sort alphabetically column2 but keeping the order of column 1 constant; much the same way that the sort function works in Excel. Regards, Wayne Dr Wayne R. Jones Statistician / Research Analyst KSS Group plc St James's Buildings 79 Oxford Street Manchester M1 6SS Tel: +44(0)161 609 4084 Mob: +44(0)7810 523 713 KSS Ltd Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS England Company Registration Number 2800886 Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305 mailto:kssg@kssg.com http://www.kssg.com The information in this Internet email is confidential and m...{{dropped}}
?order hope this helps. spencer graves Wayne Jones wrote:> Hi there R-Helpers, > > Does anyone know if it is possible to sort a dataframe? > > I.e. Sort alphabetically column 1 ( which has some reocurring elements) then > sort alphabetically column2 but keeping the order of column 1 constant; > much the same way that the sort function works in Excel. > > Regards, > > Wayne > > > Dr Wayne R. Jones > Statistician / Research Analyst > KSS Group plc > St James's Buildings > 79 Oxford Street > Manchester M1 6SS > Tel: +44(0)161 609 4084 > Mob: +44(0)7810 523 713 > > > > KSS Ltd > Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS England > Company Registration Number 2800886 > Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305 > mailto:kssg at kssg.com http://www.kssg.com > > > The information in this Internet email is confidential and m...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Wed, 2003-07-16 at 08:42, Wayne Jones wrote:> Hi there R-Helpers, > > Does anyone know if it is possible to sort a dataframe? > > I.e. Sort alphabetically column 1 ( which has some reocurring elements) then > sort alphabetically column2 but keeping the order of column 1 constant; > much the same way that the sort function works in Excel. > > Regards, > > WayneWayne, Use order() to sort the columns. This allows for multi-level keys. See ?order for more information. Example: # Create two column df> df <- data.frame(V1 = c("W","A", "A", "B", ""), V2 = c("E", "M", "B","O", "Q")) # show df unsorted> dfV1 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 Note that rows 2 and 3 are subsorted on V2. HTH, Marc Schwartz
On Wed, 16 Jul 2003 14:42:09 +0100, Wayne Jones <JonesW at kssg.com> wrote :> >Hi there R-Helpers, > >Does anyone know if it is possible to sort a dataframe? > >I.e. Sort alphabetically column 1 ( which has some reocurring elements) then >sort alphabetically column2 but keeping the order of column 1 constant; >much the same way that the sort function works in Excel.The general idea is to use order(): sorted <- unsorted[order(unsorted$col1, unsorted$col2),] Duncan Murdoch
Hi! I think this is an important example! Is there a way to make it included in the help of order? 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? 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
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