Home » How to Arrange Rows in R

How to Arrange Rows in R

by Erma Khan

Often you may be interested in arranging the rows of a data frame in R in a specific order. Fortunately this is easy to do using the arrange() function from the dplyr library.

This tutorial explains several examples of how to use this function in practice using the following data frame:

#create data frame
df #view data frame 
df

  player points assists
1      A     12       3
2      B     14       5
3      C     14       7
4      D     15       8
5      E     20      14
6      F     18      NA
7      G     29       9

Example 1: Arrange by One Column

The following code shows how to arrange the data frame in ascending order based on the values in the ‘points’ column:

library(dplyr)

df %>% arrange(points)

  player points assists
1      A     12       3
2      B     14       5
3      C     14       7
4      D     15       8
5      F     18      NA
6      E     20      14
7      G     29       9

To sort in descending order, we can use the desc() function:

df %>% arrange(desc(points))

  player points assists
1      G     29       9
2      E     20      14
3      F     18      NA
4      D     15       8
5      B     14       5
6      C     14       5
7      A     12       3

Note that NA’s will be sorted to the end, whether or not you sort ascending or decsending:

df %>% arrange(assists)

  player points assists
1      A     12       3
2      B     14       5
3      C     14       7
4      D     15       8
5      G     29       9
6      E     20      14
7      F     18      NA

df %>% arrange(desc(assists))

  player points assists
1      E     20      14
2      G     29       9
3      D     15       8
4      C     14       7
5      B     14       5
6      A     12       3
7      F     18      NA

Example 2: Arrange by Multiple Columns

To arrange the rows by multiple columns, we can simply provide more column names as arguments:

#sort by points, then assists
df %>% arrange(points, assists)

  player points assists
1      A     12       3
2      B     14       5
3      C     14       7
4      D     15       8
5      F     18      NA
6      E     20      14
7      G     29       9

We can also arrange the rows by one column ascending and another descending:

#sort by points ascending, then assists descending
df %>% arrange(points, desc(assists))

  player points assists
1      A     12       3
2      C     14       7
3      B     14       5
4      D     15       8
5      F     18      NA
6      E     20      14
7      G     29       9

Example 3: Arrange Rows in a Custom order

Occasionally you may also want to sort the rows in a custom order. You can easily do this by using a factor with specific levels:

#sort by player with custom order
df %>% arrange(factor(player, levels = c('D', 'C', 'A', 'B', 'E', 'F', 'G')))

  player points assists
1      D     15       8
2      C     14       7
3      A     12       3
4      B     14       5
5      E     20      14
6      F     18      NA
7      G     29       9

You can find the complete documentation for the arrange() function here.

Related Posts