How To Sort Or Order A Data Frame In R

This tutorial is going to illustrate how to sort or order a data frame in R.

1. Preparation

Let’s assume that we have a small data frame about books as follows, which will be used for all examples in this tutorials:

And let’s print out the dataset:

Order A Data Frame In R

2. Order A Data Frame In R Using The Order Function

To order a data frame in R, we can use the order function of the base package.

2.1. Order A Data Frame By Column Name

To order any column by name, we just need to pass it into the order function. For example, let’s order the title column of the above data frame:

Let’s see the sorted data frame:

We can see that it was sorted ascending by the title column.

Another variant syntax we can use to sort a data frame in R by column name is:

Printing out the sortedBookDF gives us the same sorted result.

2.2. Order A Data Frame By Column Index

To order a data frame by column index, we just simply need to pass the desired sorting column(s) to the order function. For example, to sort the 4th column:

Let’s see the sorted result:

Sort A Data Frame In R - Order By Index

The sorted result

2.3. Order By Ascending and Descending

By default, the order function sorts the keys by ascending. To specify the sorting should be in ascending or descending, we can pass the decreasing parameter into the order function.  For  example, to sort the title column in descending order:

Let’s see the sorted result:

Order A Data Frame In R By Ascending or Descending

Sort the title column in descending order.

To specify the sort order with the column index:

2.4. Order A Data Frame By Multiple Columns

Ordering a data frame with multiple columns is similar to ordering it by one column. The order function accepts multiple arguments, so we can give it multiple sort keys.

Continue the above example, we can sort the data frame on both column “title” and column “year” by giving both those columns to the order function:

or with a variant syntax:

or to sort a data frame in R on multiple column indexes:

3. Order A Data Frame In R Using The plyr Package

To order a data frame in R, besides the built-in order function of R, we can use the arrange function of the plyr package. The arrange function is much easier to use but does require the external package to be installed.

Firstly, let’s install the plyr package by issuing the following command:

Secondly, let’s include the package:

Next, let’s get to how to sort a data frame in R using the plyr package.

3.1. Order By Column Name

To order a data frame using the arrange function of the plyr package, we simply pass it and the column we want to sort as arguments of the function. For example, to sort the above one by the column “title“:

3.2. Order In Descending

The arrange function orders the data frame in ascending by default, to reserve the order or change the order by descending, we can combine the arrange function with the desc function of the plyr package. For example, to sort the above one by the column “title” in the descending order:

3.3. Order A Data Frame By Multiple Columns

Ordering by multiple columns with the arrange function is similar to ordering by a single column, we just need to pass the data frame and all desired columns as arguments of the function. For example, to sort the above one by both the column “title” and the column “year“:

4. Order A Data Frame In R Using The dplyr Package

Next, let’s get to how to order a data frame in R using the arrange method of the dplyr package. At first, let’s take a look at the data frame that is going to be used for all examples in this section:

Here is how it is printed out:

Secondly, let’s include the dplyr library into the environment:

4.1. Order By Column Name

To order or sort a data frame by column name, we just need to pass it and the desired column into the arrange method of the dplyr package, which is used to arrange (or re-order) rows, for example:

We have sorted the subjectDfrm data frame by the id column. Let’s see the result:

As the dplyr supports the pipe operator (%>%), let’s use it for sorting:

We get the same result with the above sorting function:

4.2. Order In Descending

In similar to the desc() function of plyr package, we can use the desc() function of the dplyr package to sort a data frame in descending order, for example:

or sorting through the pipe operator:

The sorted result now is:

We can the that the column “age” is sorted in descending order.

4.3. Order A Data Frame By Multiple Columns

To order or sort a data frame in R by multiple columns with the dplyr package, we simply pass the desired columns to the arrange method, for example, to sort the columns “id“, “age” and “weight” of the above data frame:

Or with the pipe operator:

The sorted result now is:

To sort multiple columns in the descending order:

5. Order A Data Frame In R Using The doBy Package

And next, in this section, we’re going to use the orderBy function of the doBy package to order a data frame in R. Firstly, let’s install and make the library ready in our R environment:

Secondly, let’s take a look at the syntax of the orderBy function:

Where:

  • formula
    The right hand side of a formula
  • data
    A data frame

And the sign of the terms in the formula determines whether sorting should be ascending or decreasing.

Thirdly, let’s create a data frame for all examples in this section:

And print out its content:

5.1. Order A Data Frame By Column Name

Let’s see the following example which shows how we sort the data frame by the column “chol“:

The sorted result is:

5.2. Order In Descending

To sort or order a data frame in descending order by using the orderBy function of the doBy package, we need to specify the sign (+ or -) in the formula passed to the function, for example, let’s sort the column “chol” in ascending order:

Let’s see the sorted result:

We can see that the column “chol” was sorted in reserving order compared to above example.

5.3. Order A Data Frame By Multiple Columns

To order a data frame by multiple columns using the orderBy function of the doBy package, we will need to create a formula which combines the desired columns with sorting orders, for example, let’s sort the data frame by both the columns “chol” and “id” , where the column “chol” is in ascending and the “id” is in ascending order:

And see the sorted result:

Let’s reserve the order of the column “id” to descending:

And see the sorted result:

Observe that the records S1023 and S1024 are reserved in the second example.

6. Conclusion

The tutorial has shown us how to sort or order a data frame in R by using the order, an R’s built-in function and the arrange function of the plyr,  dplyr package as well. We can see that the order function provides us flexible ways to sort a data frame in R while the arrange, orderBy functions are much easier.

Below are other R related tutorials for your references: