dplyr’s arrange(): Sort a Dataframe Columns

Sort Dataframe by a Column
Sort Dataframe by a Column in R

In this tutorial, we will learn how to sort a dataframe by a column in R. We will see three examples of using tidyverse’s arrange() function to sort a dataframe by one of its columns. First, we will sort a dataframe by numerical column in ascending order, next we will sort it in descending order using desc() function and then we will also sort a dataframe by a column that is of character type.

Sort Dataframe by a Column in R

Let us create a dataframe from scratch using two vectors. One of the vectors is a character vector and the other vector is a numerical vector.

language <- c("R","Python", "SQL","Java", "JavaScript", "Rust", "Go")
salary <- c(143000, 150000,144000, 155000, 146000, 180000,179000)

We can create a datframe using data.frame() function using the vectors.

df <- data.frame(Language= language, Salary=salary)
df
##     Language Salary
## 1          R 143000
## 2     Python 150000
## 3        SQL 144000
## 4       Java 155000
## 5 JavaScript 146000
## 6       Rust 180000
## 7         Go 179000

Sort dataframe by a column in R with arrange()

We will use arrange() function in dplyr package in tidyverse. Let us load tidyverse.

library(tidyverse)

To sort a dataframe by a variable, a column in the dataframe, we specify the variable/column name as argument to arrange() function.

df %>% 
  arrange(Salary)

##     Language Salary
## 1          R 143000
## 2        SQL 144000
## 3 JavaScript 146000
## 4     Python 150000
## 5       Java 155000
## 6         Go 179000
## 7       Rust 180000

Sort dataframe in descending order by a column in R with arrange()

Note that our dataframe is sorted by Salary values in ascending order. In order to sort the dataframe by the variable in descending order we use an additional function desc().

df %>% 
  arrange(desc(Salary))

Now our dataframe is sorted in descending order.

##     Language Salary
## 1       Rust 180000
## 2         Go 179000
## 3       Java 155000
## 4     Python 150000
## 5 JavaScript 146000
## 6        SQL 144000
## 7          R 143000

Sort dataframe by a character variable in R

In our previous two examples, we used a numerical variable to sort the dataframe. We can also sort the dataframe by other variable types as well. Here, we sort by the character variable. When we sort the dataframe by a character variable arrange() function sorts it in alphabetical order.

df %>% 
  arrange(Language)
##     Language Salary
## 1         Go 179000
## 2       Java 155000
## 3 JavaScript 146000
## 4     Python 150000
## 5          R 143000
## 6       Rust 180000
## 7        SQL 144000

And we can also use arrange() function to sort rows of a dataframe by values of multiiple columns.

Exit mobile version