How to Sort a DataFrame in R ?
Last Updated :
01 Aug, 2023
In this article, we will discuss how to sort the dataframe in R Programming Language.
In R DataFrame is a two-dimensional tabular data structure that consists of rows and columns. Sorting a DataFrame allows us to reorder the rows based on the values in one or more columns. This can be useful for various purposes, such as organizing data for analysis or presentation.
Methods to sort a dataframe:
- order() function (increasing and decreasing order)
- arrange() function from dplyr package
- 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 a 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 roll no 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 a dataframe with 3 columns named roll no, names, and subjects with a vector, displayed the Sorted dataframe based on subjects in increasing order, displayed the Sorted dataframe based on roll no 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 the 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
Similar Reads
How to Sort a DataFrame by Date in R?
In this article, we will discuss how to sort a dataframe in R Programming Language. We can create a dataframe in R by using data.frame() function. In a dataframe we can create a date column using as.Date() function in the '%m/%d/%Y' format. Example: Let's create a dataframe with 2 columns including
2 min read
How to split DataFrame in R
In this article, we will discuss how to split the dataframe in R programming language. A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be r
4 min read
How to Transpose a Data Frame in R?
Transposing means converting rows to columns and columns to rows of a data frame. Transposing can be useful for various purposes, such as reshaping data or preparing it for specific analyses. Transpose a Data Frame in R Using t() functionHere we are using t() function which stands for transpose to T
3 min read
How To Merge Two DataFrames in R ?
In this article, We are going to see how to merge two R dataFrames. Merging of Data frames in R can be done in two ways. Merging columnsMerging rowsMerging columns In this way, we merge the database horizontally. We use the merge function to merge two frames by one or more common key variables(i.e.,
2 min read
How to Unnest dataframe in R ?
In this article, we will discuss how to unnest dataframes in R Programming Language. Unnesting of dataframe refers to flattening it. Method 1: Using do.call approach The do.call() method in base R constructs and executes a function call from a function using its corresponding argument list. Syntax
3 min read
How to add Header to Dataframe in R ?
A header necessarily stores the names or headings for each of the columns. It basically helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. In this article, we will learn how to add a Header t
3 min read
How to add column to dataframe in R ?
In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n)Â Here c() function is a vec
2 min read
How to Remove Rows in R DataFrame?
In this article, we will discuss how to remove rows from dataframe in the R programming language. Method 1: Remove Rows by Number By using a particular row index number we can remove the rows. Syntax: data[-c(row_number), ] where. data is the input dataframerow_number is the row index position Exam
2 min read
How to Stack DataFrame Columns in R?
A dataframe is a tubular structure composed of rows and columns. The dataframe columns can be stacked together to divide the columns depending on the values contained within them. Method 1: Using stack method The cbind() operation is used to stack the columns of the data frame together. Initially,
3 min read
How to add a row to R dataframe ?
In this article, we will see how to add rows to a DataFrame in R Programming Language. To do this we will use rbind() function. This function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. Syntax: rbind(dataframe 1, dataframe 2) Example 1 : [GFGTABS] R # creating a
2 min read