You are currently viewing dplyr arrange() Function in R

How to use the dplyr arrange() function in R? The 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.

Advertisements

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

The arrange() function in R, provided by the dplyr package, sorts the rows of a data frame by the values of specified columns. By default, it sorts in ascending order, but it can also sort in descending order. To use the arrange() function, you need to first install the dplyr package using install.packages(‘dplyr’) and then load it with library(dplyr).

All functions in the dplyr package take a data.frame as their first argument. When using the dplyr package, we commonly utilize the infix operator %>% from the magrittr package. This operator passes the left-hand side value to the first argument of the function on the right-hand side. For instance, x %>% f(y) is transformed into f(x, y), effectively “piping” the result from the left-hand side into the right-hand side function.


# 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’s arrange() function orders in ascending order however, you can change this in R and arrange the dataframe in descending/decreasing order by using the 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