Sorting DataFrame in R using Dplyr - GeeksforGeeks

28 Jul, 2021
In this article, we will discuss about how to sort a dataframe in R programming language using Dplyr package. The package Dplyr in R programming language provides a function called arrange() function which is useful for sorting the dataframe.
Syntax : 
arrange(.data, ...)
The methods given below show how this function can be used in various ways to sort a dataframe.
Sorting in ascending order is the default sorting order in arrange() function. The attribute to sort by should be given as an argument to this function.
Example: Sorting the dataframe in ascending order
R
# Installing the loading the packageinstall.packages("dplyr")  library(dplyr)  # Creating dataframegfg = data.frame(Customers = c("Roohi", "James", "Satish", "Heera",                                "Sehnaaz", "Joe","Raj", "Simran",                                "Priya","Tejaswi"),                                    Product = c("Product A", "Product B", "Product C",                             "Product A", "Product D", "Product B",                             "Product D", "Product C", "Product D",                             "Product A"),                                    Salary = c(514.65, 354.99, 345.44, 989.56, 767.50,                            576.90, 878.67, 904.56,123.45, 765.78)                )  gfg                  # Sorting the dataframe in ascending orderarrange(gfg, Salary)
Output :
For sorting our dataframe in descending order, we will use desc() function along with the arrange() function. We will also use % operator for comparison of the dataframe column which we are taking for sorting purpose.
Example: Sorting the dataframe in descending order
R
library(dplyr)  # Creating dataframegfg = data.frame(Customers = c("Roohi", "James", "Satish", "Heera",                               "Sehnaaz", "Joe", "Raj", "Simran",                                "Priya", "Tejaswi"),                                    Product = c("Product A", "Product B", "Product C",                             "Product A", "Product D", "Product B",                             "Product D", "Product C", "Product D",                             "Product A"),                                    Salary = c(514.65, 354.99, 345.44, 989.56, 767.50,                             576.90, 878.67, 904.56,123.45, 765.78))                 # Sorting the dataframe in descending # ordergfg %>% arrange(desc(Salary))
Output :
We will now sort our dataframe using multiple variables using the arrange() function. The attributes should be given to the function separated by a column. For example, in the given example the dataframe is sorted by salary column in descending order and product column in ascending order. We will use the % operator for comparing data to be sorted in descending order.
Example: Sorting the dataframe using multiple variables
R
library(dplyr)  # Creating dataframegfg = data.frame(Customers = c("Roohi", "James", "Satish", "Heera",                               "Sehnaaz", "Joe", "Raj", "Simran",                                "Priya", "Tejaswi"),                                    Product = c("Product A", "Product B", "Product C",                             "Product A", "Product D", "Product B",                             "Product D", "Product C", "Product D",                              "Product A"),                                    Salary = c(514.65, 354.99, 345.44, 989.56, 767.50,                             576.90, 878.67, 904.56,123.45, 765.78))                 # Sorting the dataframe using # multiple variablesgfg %>% arrange(Product, desc(Salary))
Output :
Picked
R Dplyr
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R
R - if statement
How to filter R dataframe by multiple conditions?