Sorting DataFrame in R using Dplyr – arrange function

Sorting dataframe in R can be done using Dplyr. Dplyr package in R is provided with arrange() function which sorts the dataframe by multiple conditions. We will provide example on how to sort a dataframe in ascending order and descending order. how to sort a dataframe by column name.  Difference between order and sort in R etc. We will start with sorting a list and vector in R.

  • sort a vector in R using sort() function in R – Sort Vector in descending order and ascending order
  • Sort dataframe by column name and column position
  • Sort dataframe by multiple columns in descending order and ascending order.
  • Sort a dataframe using order function in R

Sort a Vector in R :

Sort dataframe in R using dplyr arrange() order() 5

sort() function is pretty simple and straight forward function which sorts the vector in R in both descending order and in ascending order let’s see an example of both the cases.

Sort() function in R – Sort Vector in Ascending order

sort() function takes up vector as input and outputs the ascending sorted vector.

#### Sort vector in ascending order

v1 <- c(4,21,5,23,7,3,1,12)
sort(v1)

the default sort is ascending order, so the sorted vector will be

[1] 1  3  4  5  7   12   21   23

Sort() function in R – Sort Vector in Descending order

sort() function takes up vector as input argument and decreasing = TRUE as another argument there by sorts the vector in descending order.

v1 <- c(4,21,5,23,7,3,1,12)

### sort vector in descending order
sort(v1,decreasing = TRUE)

So the descending sorted vector will be

 [1]  23   21   12   7   5  4  3  1

 

 


Sorting a Dataframe in R Dplyr

Let’s first create the dataframe.

df1= data.frame(Name=c('James','Paul','Richards','Marico','Samantha','Ravi','Raghu','Richards','George','Ema','Samantha','Catherine'),
                State=c('Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'),
                Sales=c(14,24,24,32,13,7,9,41,18,16,28,14))
df1

So the resultant dataframe will be

Sort the column of dataframe in R 1


Sort dataframe using Arrange() function in R Dplyr:

Sort dataframe in R using dplyr arrange() order() 1

 Sort in Ascending order: Example of sorting with arrange() function

library(dplyr)

# sort the dataframe in R using arrange
arrange(df1,Sales)

The default sorting order of arrange() function is ascending so the resultant dataframe will be sorted in ascending order.

Sort the column of dataframe in R 2

 

Sort in descending order: Example of sorting in descending order with arrange() function

Sort dataframe in R using dplyr arrange() order() 2

library(dplyr)

# sort the dataframe in R using arrange
df1 %>% arrange(desc(Sales))

The arrange() function along with desc() is used to sort the dataframe in descending order  so the resultant dataframe will be.

Sort the column of dataframe in R 3

 

Sorting dataframe in R using multiple variables with Dplyr:

In this example, we are sorting data by multiple variables. One in descending and one in ascending the example is shown below. We will be using pipe operator  (%>%).

# sort the dataframe in R using multiple variable with Dplyr
df1 %>% arrange(desc(State),Sales)

pipe operator first sorts the “State” column in descending order format and within that it sorts “Sales” in ascending order format.

Sort dataframe in R using dplyr arrange() order() 3

 

 


Sort dataframe using Order function in R:

Sort the column of dataframe in R by ascending order:

Sorting the column is done with the help of order function. Default is ascending order

## Sort the column by ascending

df2 <- df1[order(df1$Sales),] 
df2

So the sorted dataframe will be

Sort the column of dataframe in R 2

 

Sort the column of dataframe in R by descending order:

Sorting the column is done with the help of order function, specifying minus symbol orders the column in descending order as shown

## Sort the column by descending

df2 <- df1[order(-df1$Sales),] 
df2

So the sorted dataframe will be

Sort the column of dataframe in R 3

 

Sort by multiple column of dataframe in R:

In below example lets first sort by column “state” in ascending order and then sort by “sales” in descending order

## Sort by multiple column 

df2 <- df1[order(df1$State,-df1$Sales),]
df2

So the sorted dataframe will be

Sort the column of dataframe in R 4

 


Sort the dataframe by  Column Positions in R

Sort  dataframe in R by single column position:

Sorting the dataframe by  column position is done with the help of order function. Default is ascending order

## Sort the column by ascending

df2 <- df1[order(df1[,3]),] 
df2

So the dataframe sorted on 3rd position will be

Sort the column of dataframe in R 2

 

Sort  dataframe in R by multiple column positions:

Sorting the dataframe by multiple column position is done with the help of order function. Default is ascending order

## Sort the column by ascending

df2 <- df1[order(df1[,2],df1[,3]),] 
df2

First 2nd column (“State”) is sorted in Ascending order and then within that 3rd column(“Sales”) is sorted again in ascending order. So the resultant dataframe will be

Sort dataframe in R using dplyr arrange() order() 4

For further understanding on sorting with the help of dplyr package refer the documentation


Other Related Topics:

Sorting DataFrame in R using Dplyr                                                                                                          Sorting DataFrame in R using Dplyr

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts