VOOZH about

URL: https://www.geeksforgeeks.org/r-language/dplyr-arrange-function-in-r/

⇱ dplyr arrange() Function in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

dplyr arrange() Function in R

Last Updated : 23 Jul, 2025

In data analysis and manipulation, arranging data according to specific criteria is a fundamental operation. Whether it's sorting a dataset by a certain column or multiple columns, this task is often essential for gaining insights and making informed decisions. In R Programming Language the dplyr package provides a powerful set of tools for data manipulation, and the arrange() function is one such tool that facilitates data sorting within data frames. This article aims to provide a comprehensive understanding of the arrange() function in R's dplyr package.

How to use the arrange() function

The arrange() function is used to reorder rows of a data frame based on one or more columns. It sorts the rows in ascending or descending order of the specified variables. This function is particularly useful for tasks such as identifying trends, identifying outliers, or preparing data for visualization.

The syntax of the arrange() function is

arrange(.data, ..., .by_group = FALSE)
  • data: The input data frame.
  • ...: Comma-separated expressions indicating the variables to arrange by.
  • by_group: A logical value indicating whether to preserve grouping information. Defaults to FALSE.

Arrange values by a Single Variable

Suppose you have a dataset containing information about students' exam scores. You want to arrange the data by their scores in ascending order to identify the highest and lowest scorers.

Output:

 Name Score
1 Charlie 78
2 Ali 85
3 Boby 92
4 Davdas 95

Arrange values by a Multiple Variables

Consider a dataset of sales transactions, where you want to arrange the transactions first by the transaction date in ascending order and then by the amount in descending order to identify the largest transactions on each day.

Output:

 Date Amount
1 2024-04-01 100
2 2024-04-01 150
3 2024-04-02 200
4 2024-04-03 75

Arrange by Date in ascending order, then by Amount in descending order

 Date Amount
1 2024-04-01 150
2 2024-04-01 100
3 2024-04-02 200
4 2024-04-03 75

Arrange values with Missing Values

Suppose you have a dataset with missing values and you want to arrange the data by a variable, but you want to place missing values at the beginning of the ordering.

Output:

 ID Value
1 1 20
2 2 NA
3 NA 15
4 4 30

Arrange by Value in ascending order, placing missing values first

 ID Value
1 2 NA
2 NA 15
3 1 20
4 4 30

Conclusion

The arrange() function in R's dplyr package provides a convenient way to sort data frames based on one or more variables. Its intuitive syntax and flexibility make it a valuable tool for data manipulation tasks. By mastering arrange() and other functions in dplyr, analysts can streamline their workflows and gain deeper insights from their data.

Comment
Article Tags:
Article Tags:

Explore