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.
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, IfTRUE
, 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.
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.
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.
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
Frequently Asked Questions of R dplyr arrange()
The arrange()
function in dplyr
is used to reorder the rows of a data frame based on the values in one or more columns.
By default, arrange()
sorts rows in ascending order. Simply pass the column name(s) to the function:
Yes, arrange()
is often combined with functions like filter()
, mutate()
, and select()
for comprehensive data manipulation:
To sort rows in descending order, use the desc()
function within arrange()
.
By default, arrange()
places NA
values at the end of the data frame. If you need a different behavior, you can use additional logic to handle NA
values explicitly.
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
- Sort or Order List in R?
- R Sort by Multiple Columns
- How to Sort by Date in R?
- R Sort DataFrame Rows by Column Value
- Order DataFrame by one descending and one ascending column in R
- R Sort Vector
- Reorder Columns of DataFrame in R
- R lm() Function – Fitting Linear Models
- Reorder Columns of DataFrame in R
- R Sort DataFrame Rows by Multiple Columns