You are currently viewing dplyr arrange() Function in R

How to use the dplyr arrange() function in R? arrange() function in R is from the dplyr package that is used to order/sort the dataframe rows in either ascending or descending based on column value.

1. Syntax of dplyr arrange() Function

The following is the syntax of the dplyr arrange() function.


# Syntax of arrange()
arrange(.data, ..., .by_group = FALSE)

Here,

  • .data – A dataframe or extension dataframe for example tibble, lazy dataframes.
  • ... – columns or functions to sort in ascending order. use desc(column) to sort in descending order.
  • .by_group – Default set to FALSE, If TRUE, it will sort by grouping variable first.

2. R arrange() Ascending Order

R arrange() function by default sorts the dataframe in ascending order based on column values. The arrange() function from the dplyr package is used to order the rows of a data frame by the values of selected columns in either ascending or descending order. To use arrange() function, you have to install dplyr first using install.packages(‘dplyr’) and load it using library(dplyr).

All functions in dplyr package take data.frame as a first argument. When we use dplyr package, we mostly use the infix operator %>% from magrittr, it passes the left-hand side of the operator to the first argument of the right-hand side of the operator. For example, x %>% f(y) converted into f(x, y) so the result from the left-hand side is then “piped” into the right-hand side. 


# Create Data Frame
df=data.frame(id=c(11,22,33,44,55),
              name=c("spark","python","R","jsp","java"),
              price=c(144,NA,321,567,567),
              publish_date= as.Date(
                c("2007-06-22", "2004-02-13", "2006-05-18",
                  "2010-09-02","2007-07-20"))
)

# Load dplyr library
library(dplyr)

# Using arrange in ascending order
df2 <- df %>% arrange(price)
df2

Yields the below output. The above example orders the dataframe by column price in ascending order.

r dplyr arrange ascending

3. R arrange() Descending Order

By default, dplyr arrange() function orders in ascending order however, you can change this in R and arrange the dataframe in descending/decreasing order by using desc() function. The desc() takes the column name as an argument and orders the values in descending order.


# Using arrange in descending order
df2 <- df %>% arrange(desc(price))
df2

Yields the below output. The above example orders the dataframe by column price in descending order.

r dplyr arrange descending

4. dplyr arrange Multiple Columns

By using dplyr arrange() function you can also perform ordering on multiple columns. While doing this, you can also specify one column in ascending and another column in descending order.


# Using arrange by multiple columns
df2 <- df %>% arrange(price,desc(id))
df2

Yields below output.

dplyr arrange multiple columns

5. Complete Example

Following is a complete example of using arrange() function in R.


# Create Data Frame
df=data.frame(id=c(11,22,33,44,55),
              name=c("spark","python","R","jsp","java"),
              price=c(144,NA,321,567,567),
              publish_date= as.Date(
                c("2007-06-22", "2004-02-13", "2006-05-18",
                  "2010-09-02","2007-07-20"))
)
df

# Load dplyr library
library(dplyr)

# Using arrange in ascending order
df2 <- df %>% arrange(price)
df2

# Using arrange in descending order
df2 <- df %>% arrange(desc(price))
df2

# Using arrange by multiple columns
df2 <- df %>% arrange(price,desc(id))
df2

7. Conclusion

In this R dplyr article, I have explained what is arrange function is, how to use it to order dataframe rows in ascending or descending by column values, and finally order multiple columns.

Related Articles

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium