How can I arrange rows in a custom order using dplyr?

Using the dplyr package in R, you can arrange the rows of a data frame in a custom order by using the arrange() function. This function allows you to specify the order of the rows based on one or more of the columns in the data frame. You can also use the desc() function to sort the rows in descending order based on a column. Finally, you can use the reorder() function to rearrange the rows in the data frame based on a specified column.


You can use the following basic syntax to arrange the rows in a data frame in a custom order using the dplyr package in R:

library(dplyr)

#arrange rows in custom order based on values in 'team' column
df %>%
  arrange(match(team, c('C', 'B', 'D', 'A')), points)

This particular example arranges the rows based on the custom order of C, B, D, A for values in the team column, then by the values in the points column ascending.

The following example shows how to use this syntax in practice.

Example: How to Arrange Rows in Custom Order Using dplyr

Suppose we have the following data frame that shows the points scored by basketball players on various teams:

#create data frame
df <- data.frame(team=c('A', 'B', 'A', 'A', 'B', 'D', 'C', 'D', 'D', 'C'),
                 points=c(12, 20, 14, 34, 29, 22, 28, 15, 20, 13))

#view data frame
df

   team points
1     A     12
2     B     20
3     A     14
4     A     34
5     B     29
6     D     22
7     C     28
8     D     15
9     D     20
10    C     13

If we use the arrange() function to order the rows based on the values in the team column, then by the values in the points column, the arrange() function will order the rows based on alphabetical order by default:

library(dplyr)

#arrange rows in ascending order by team, then by points
df %>%
  arrange(team, points)

   team points
1     A     12
2     A     14
3     A     34
4     B     20
5     B     29
6     C     13
7     C     28
8     D     15
9     D     20
10    D     22

The rows are arranged in alphabetical order by team, then ascending order by points.

However, suppose we would instead like to arrange the rows based on the following order of team values: C, B, D, A.

We can use the match() function within the arrange() function to do so:

library(dplyr)

#arrange rows in custom order based on 'team' column, then by 'points' column
df %>%
  arrange(match(team, c('C', 'B', 'D', 'A')), points)

   team points
1     C     13
2     C     28
3     B     20
4     B     29
5     D     15
6     D     20
7     D     22
8     A     12
9     A     14
10    A     34

The rows are arranged in the custom order that we specified (C, B, D, A) for the team column, then by the points column.

Note #1: The match function gets the row index of values in the team column and then the arrange function is able to order based on these index values.

Note #2: To arrange based on points values descending, simply use desc(points) instead.

x