How to Sort a DataFrame in R ? - GeeksforGeeks

30 May, 2021
In this article, we will discuss how to sort the dataframe in R Programming Language. 
Methods to sort a dataframe:
order() function (increasing and decreasing order)arrange() function from dplyr packagesetorder() function from data.table package
order() function (increasing and decreasing order)
arrange() function from dplyr package
setorder() function from data.table package
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 columnsdata = 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 columnsdata=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
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 packagelibrary("dplyr")    # create dataframe with roll no, names # and subjects columnsdata = 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 subjectsprint(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
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 librarylibrary("data.table")  # create dataframe with roll no, names # and subjects columnsdata=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 subjectsprint(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
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
Data Visualization in R
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R