R Order to Sort Data

This page will show you how to sort a data frame in R using the order command. Sort your data by one or more fields from low to high or high to low. R order is used for data frames. To sort other types of objects use the sort command.

Get Example Data for R Order

If you want to follow along with the examples below you will need the data that is used. To get this data, install and load the eeptools package and then open the sample data by running the following code in R:

# Install eeptools
install.packages("eeptools")

# Load eeptools
library(eeptools)

# Open data
data(stulevel)

Example 1: Sort Non-Text Data in R by One Column Lowest to Highest

The first example shows how to sort data using R order on just one column. By default R order sorts from lowest to highest. Note that the first four examples will work on numeric, integer or factor fields. If you want to sort on a character field go to example 5.

# Sort data by one column
stulevel <- stulevel[order(stulevel$ability) , ]

Example 2: Sort Non-Text Data in R by One Column Highest to Lowest

The second example shows how to sort data again using R order on one column but this time sorting from highest to lowest. This is specified using the negative sign in front of the name of the field you want to sort on.

# Sort data by one column highest to lowest
stulevel <- stulevel[order(-stulevel$ability) , ]

Example 3: Sort Non-Text Data in R by Two Columns Lowest to Highest

The third example shows how to sort data using R order on two columns. You can sort on as many columns as you want to by adding a comma and then listing the column within the parentheses.

# Sort data by two columns lowest to highest
stulevel <- stulevel[order(stulevel$grade, stulevel$ability) , ]

Example 4: Sort Non-Text Data in R by Two Columns Highest to Lowest

The fourth example shows how to sort data using R order on two columns but sorting highest to lowest. Again, just place a negative sign in front of any columns that you want to use to sort from high to low. This example will sort grade from high to low but ability from low to high.

# Sort data by two columns highest to lowest
stulevel <- stulevel[order(-stulevel$grade, stulevel$ability) , ]

Example 5: Sort Text Data in R by One Column Lowest to Highest

The fifth example shows how to sort data using R order on just one column. This code is needed for character fields but will also work for other types of data. Of course, you could also convert the character data to factor and then convert back.

# First create some example character data
stulevel$proflvl_character <- as.character(stulevel$proflvl)
stulevel$race_character <- as.character(stulevel$race)

# Sort character data by one column lowest to highest
stulevel <- stulevel[with(stulevel, order(stulevel$proflvl_character)),]

Example 6: Sort Text Data in R by One Column Highest to Lowest

The sixth example shows how to sort data using R order on just one column from highest to lowest. Note that putting a negative sign before the name of the field won’t work.

# Sort character data by one column highest to lowest
stulevel <- stulevel[order(stulevel$proflvl_character, decreasing=TRUE),]

Example 7: Sort Text Data in R by Two Columns Lowest to Highest

The seventh example shows how to sort data using R order on two columns from lowest to highest.

# Sort character data by two columns lowest to highest
stulevel <- stulevel[with(stulevel, order(stulevel$proflvl_character, stulevel$race_character)),]

 Example 8: Sort Text Data in R by Two Columns Highest to Lowest

The seventh example shows how to sort data using R order on two columns from highest to lowest. Note that putting a negative sign before the name of the field won’t work.

# Sort character data by two columns highest to lowest
stulevel <- stulevel[with(stulevel, order(stulevel$proflvl_character, stulevel$race_character, decreasing=TRUE)),]

What about sorting by two character fields if you want one field to be increasing and the other decreasing? Good question. I’ll post it here when I find out how.

R Order

There are other options that can be used with R order. See official R-manual page on data.table to learn more: http://stat.ethz.ch/R-manual/R-patched/library/base/html/order.html.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.

JM

2 thoughts on “R Order to Sort Data

  1. Stefan

    I was stuck on this, too:

    “What about sorting by two character fields if you want one field to be increasing and the other decreasing? Good question. I’ll post it here when I find out how.”

    Turns out the answer is in the official R menu page, by using xtfrm:

    ## More generally we can make use of xtfrm
    cy <- as.character(y)
    rbind(x, y, z)[, order(x, -xtfrm(cy), z)]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *