Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Sort a DataFrame in R ?

  • Last Updated : 30 May, 2021

In this article, we will discuss how to sort the dataframe in R Programming Language. 

Methods to sort a dataframe:

  1. order() function (increasing and decreasing order)
  2. arrange() function from dplyr package
  3. setorder() function from data.table package

Method 1: Using order() function

This function is used to sort the dataframe based on the particular column in the dataframe

Syntax: order(dataframe$column_name,decreasing = TRUE))

where 



  • dataframe is the input dataframe
  • Column name is the column in the dataframe such that dataframe is sorted based on this column
  • Decreasing parameter specifies the type of sorting order

If it is TRUE dataframe is sorted in descending order. Otherwise, in increasing order

return type: Index positions of the elements

Example 1: R program to create dataframe with 2 columns and order based on particular columns in decreasing order. Displayed the Sorted dataframe based on subjects in decreasing order, displayed the Sorted dataframe based on rollno in decreasing order

R




# create dataframe with roll no and 
# subjects columns
data = data.frame(
  rollno = c(1, 5, 4, 2, 3),
  subjects = c("java", "python", "php", "sql", "c"))
  
print(data)
  
print("sort the data in decreasing order based on subjects ")
print(data[order(data$subjects, decreasing = TRUE), ]   )
  
  
print("sort the data in decreasing order based on rollno ")
print(data[order(data$rollno, decreasing = TRUE), ]   )

Output:

  rollno subjects
1      1     java
2      5   python
3      4      php
4      2      sql
5      3        c
[1] "sort the data in decreasing order based on subjects "
  rollno subjects
4      2      sql
2      5   python
3      4      php
1      1     java
5      3        c
[1] "sort the data in decreasing order based on rollno "
  rollno subjects
2      5   python
3      4      php
5      3        c
4      2      sql
1      1     java

Example 2: R program to create dataframe with 3 columns named rollno, names, and subjects with a vector, displayed the Sorted dataframe based on subjects in increasing order, displayed the Sorted dataframe based on rollno in increasing order, displayed the Sorted dataframe based on names in increasing order

R




# create dataframe with roll no, names
# and subjects columns
data=data.frame(rollno = c(1, 5, 4, 2, 3),
                names = c("sravan", "bobby",
                          "pinkey", "rohith"
                          "gnanesh"),
                subjects = c("java", "python",
                             "php", "sql", "c"))
  
print(data)
  
print("sort the data in increasing order based on subjects"
print(data[order(data$subjects, decreasing = FALSE), ]   )
  
  
print("sort the data in increasing order based on rollno")
print(data[order(data$rollno, decreasing = FALSE), ]   )
  
print("sort the data in increasing order based on names")
print(data[order(data$names,decreasing = FALSE), ]   )

Output:



  rollno   names subjects
1      1  sravan     java
2      5   bobby   python
3      4  pinkey      php
4      2  rohith      sql
5      3 gnanesh        c
[1] "sort the data in increasing order based on subjects"
  rollno   names subjects
5      3 gnanesh        c
1      1  sravan     java
3      4  pinkey      php
2      5   bobby   python
4      2  rohith      sql
[1] "sort the data in increasing order based on rollno"
  rollno   names subjects
1      1  sravan     java
4      2  rohith      sql
5      3 gnanesh        c
3      4  pinkey      php
2      5   bobby   python
[1] "sort the data in increasing order based on names"
  rollno   names subjects
2      5   bobby   python
5      3 gnanesh        c
3      4  pinkey      php
4      2  rohith      sql
1      1  sravan     java

Method 2: Using arrange() Function from dplyr.

Arrange() is used to sort the dataframe in increasing order, it will also sort the dataframe based on the column in the dataframe

Syntax: arrange(dataframe,column)

where

  • dataframe is the dataframe input
  • column is the column name , based on this column dataframe is sorted

We need to install dplyr package as it is available in that package

Syntax: install.packages(“dplyr”)

Example: R program to sort dataframe based on columns

In this program, we created three columns using the vector and sorted the dataframe based on the subjects column

Code:

R




# load the package
library("dplyr")  
  
# create dataframe with roll no, names 
# and subjects columns
data = data.frame(rollno = c(1, 5, 4, 2, 3),
                  names = c("sravan", "bobby", "pinkey",
                            "rohith", "gnanesh"),
                  subjects = c("java", "python", "php",
                               "sql", "c"))
  
# sort the data  based on subjects
print(arrange(data, subjects))

Output:



  rollno   names subjects
1      3 gnanesh        c
2      1  sravan     java
3      4  pinkey      php
4      5   bobby   python
5      2  rohith      sql

Method 3: Using setorder() from data.table package

setorder is used to sort a dataframe in the set order format.

Syntax: setorder(dataframe, column)

  • Where dataframe is the input dataframe
  • The column is the column name

Example: R program to sort dataframe based on columns

In this program, we created the dataframe with three columns using vector and sorted the dataframe using setorder function based on subjects column

Code:

R




# load the library
library("data.table")
  
# create dataframe with roll no, names 
# and subjects columns
data=data.frame(rollno = c(1, 5, 4, 2, 3),
                names = c("sravan", "bobby"
                          "pinkey", "rohith",
                          "gnanesh"),
                subjects = c("java", "python",
                             "php", "sql", "c"))
  
# sort the data  based on subjects
print(setorder(data,subjects))

Output:

  rollno   names subjects
5      3 gnanesh        c
1      1  sravan     java
3      4  pinkey      php
2      5   bobby   python
4      2  rohith      sql



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!