Sort Data Frame in R (4 Examples) | Order & Rearrange, dplyr & data.table (2024)

This article explains how to sort a data frame in the R programming language.

I will show you four different examples for the ordering of data frames. More precisely, the tutorial will contain the following contents:

  • Creation of Example Data
  • Example 1: Sort Data Frame with Base R (order Function)
  • Example 2: Sort Data Frame with dplyr Package (arrange Function)
  • Example 3: Sort Data Frame with data.table Package (setorder Function)
  • Example 4: Sort Data Frame in Decreasing Order

Let’s get started…

Creation of Example Data

We are going to use the following data set for the examples of this R tutorial:

data <- data.frame(x1 = 1:5, # Create example data x2 = c("A", "D", "C", "A", "d"))data # Print example data

Sort Data Frame in R (4 Examples) | Order & Rearrange, dplyr & data.table (1)

Table 1: Exemplifying Data Frame.

Table 1 shows how our example data frame looks like. Our data contains two columns (i.e. x1 and x2) and five rows.

Table 2 illustrates how our example data should be rearranged, if we want to order it according to the x2 column. The second row should be moved to the bottom and the fourth row should become the second row:

Sort Data Frame in R (4 Examples) | Order & Rearrange, dplyr & data.table (2)

Table 2: How Data Frame Should be Ordered.

Let’s do this rearrangement in R…

Example 1: Sort Data Frame with Base R (order Function)

The Base R installation already provides a good solution for the ordering of our data. We simply need to apply the order function to the column vector according to which we want to sort our data (i.e. x2). Have a look at the following R code:

Sort Data Frame in R (4 Examples) | Order & Rearrange, dplyr & data.table (3)

Table 3: Ordered Data Frame.

As you can see based on Table 3, our data is perfectly ordered in respect to the second column.

However, depending on your personal preferences you might prefer another solution for the sorting of your data. In the following, I’m therefore going to show you some programming alternatives for the ordering of data frames…

Example 2: Sort Data Frame with dplyr Package (arrange Function)

The dplyr package is a very powerful package for the manipulation of data frames and the package also provides functions for the ordering of a data matrix. Let’s install and load the package to R:

install.packages("dplyr") # Install dplyr R packagelibrary("dplyr") # Load dplyr R package

Now, we can use the arrange function of the dplyr package as follows:

arrange(data, x2) # Order data with dplyr

Have a look at the output of your RStudio console: It will show an ordered data frame in the same way as we sorted it in Example 1.

Example 3: Sort Data Frame with data.table Package (setorder Function)

Another popular package for the handling of data sets is the data.table package. First, we need to install and load the R add-on package:

install.packages("data.table") # Install data.table R packagelibrary("data.table") # Load data.table R package

In contrast to the previous R codes, the data.table package changes the original data frame automatically. Let’s duplicate our example data in order to retain an original version of the data.

data_ordered <- data # Replicate example data

Now, we can sort and print this data frame with the setorder function as follows:

setorder(data_ordered, x2) # Order data with data.tabledata_ordered # Print ordered data

Same output as before – Looks good!

Example 4: Sort Data Frame in Decreasing Order

So far, we have ordered our data only in increasing order (i.e. from low to high values). However, we can also sort data frames in descending order:

data[order(data$x2, decreasing = TRUE), ] # Order data in decreasing order

As you can see based on the output of your RStudio console, our example data was ordered alphabetically from high to low letters.

Video & Further Resources

I have published the examples of this tutorial in a video on the Statistics Globe YouTube channel. You can watch the video below:

In addition, you might have a look at some of the related R tutorials of my homepage. You can find a selection of tutorials below:

  • Sort Data Frame by Multiple Columns
  • Reorder Columns of Data Frame in R
  • Reverse Data with the rev Function
  • Subsetting Data Frame in R
  • The R Programming Language

I hope this tutorial taught you how to sort and arrange the rows of a dataframe by column values in the R programming language. In case you have further questions, let me know in the comments.

Leave a Reply

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Related Tutorials

Merge Two Matrices by Columns in R (2 Examples)

Convert All Character String Variables in Data Frame to Uppercase in R (Example)

Sort Data Frame in R (4 Examples) | Order & Rearrange, dplyr & data.table (2024)
Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5612

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.