How to Arrange Rows in Custom Order Using dplyr


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.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Arrange Rows by Group Using dplyr
How to Filter for Unique Values Using dplyr
How to Filter by Multiple Conditions Using dplyr

Leave a Reply

Your email address will not be published. Required fields are marked *