Sorting data may be a necessity while dealing with a data set. In this tutorial, we will learn how to sort a data frame by a single column and multiple columns in an increasing or decreasing order. Find out how to sort a data frame in R.

Sorting data may be essential for R users to examine the higher or lower values of the data. Firstly, we learn how to sort data frame in increasing order by a column. Secondly, we work on how to sort a data frame in decreasing order by a single variable. Thirdly, we go over how to order a data frame by multiple columns in increasing order. Last, we learn how to sort a data frame with decreasing order by multiple variables in R.

Let’s first construct an example data frame.

var1 <- c(3,1,2,1,2,2,3,1,3)
var2 <- c(33,11,33,22,33,11,22,11,33)
var3 <- c(111,222,222,111,111,333,222,333,333)
data <- data.frame(var1, var2, var3)
data
##   var1 var2 var3
## 1    3   33  111
## 2    1   11  222
## 3    2   33  222
## 4    1   22  111
## 5    2   33  111
## 6    2   11  333
## 7    3   22  222
## 8    1   11  333
## 9    3   33  333

Check Out: How to Remove Outliers from Data in R

How to Sort a Data Frame by a Single Variable in R

We can sort our data frame with order() function by single variable. 

data[order(data$var1),]
##   var1 var2 var3
## 2    1   11  222
## 4    1   22  111
## 8    1   11  333
## 3    2   33  222
## 5    2   33  111
## 6    2   11  333
## 1    3   33  111
## 7    3   22  222
## 9    3   33  333

Also Check: How to Test for Identifying Outliers in R

We can sort our data frame with order() function in decreasing order by equalizing the decreasing argument to TRUE.

data[order(data$var1, decreasing = TRUE),]
##   var1 var2 var3
## 1    3   33  111
## 7    3   22  222
## 9    3   33  333
## 3    2   33  222
## 5    2   33  111
## 6    2   11  333
## 2    1   11  222
## 4    1   22  111
## 8    1   11  333

Also Check: How to Clean Data in R

How to Sort a Data Frame by Multiple Variables in R

We can sort our data frame based on multiple variables with order() function. The order is made based on your variables. Therefore, you need to include variable names in order() function which order you prefer.

data[order(data$var1, data$var2, data$var3),]
##   var1 var2 var3
## 2    1   11  222
## 8    1   11  333
## 4    1   22  111
## 6    2   11  333
## 5    2   33  111
## 3    2   33  222
## 7    3   22  222
## 1    3   33  111
## 9    3   33  333

We can sort our data frame based on multiple variables with order() function in decreasing order by equalizing the decreasing argument to TRUE.

data[order(data$var1, data$var2, data$var3, decreasing = TRUE),]
##   var1 var2 var3
## 9    3   33  333
## 1    3   33  111
## 7    3   22  222
## 3    2   33  222
## 5    2   33  111
## 6    2   11  333
## 4    1   22  111
## 8    1   11  333
## 2    1   11  222

The application of the codes is available in our youtube channel below.

How to Sort a Data Frame by Single and Multiple Columns in R
Subscribe to YouTube Channel

Don’t forget to check: How to Handle Missing Values in R


Dr. Osman Dag