dplyr: How to Use arrange() with String


You can use the arrange() function in the dplyr package in R to order rows based on column values.

Often you may want to use the arrange() function by passing a column name as a string.

You can use the following syntax to do so:

library(dplyr)

#arrange rows in descending order based on values in team column
df %>% arrange(desc(!!sym('team')))

Note that the syntax !!sym allows us to convert a string to a symbol and print it without quotes.

This particular example allows us to pass a string to the desc() argument within the arrange() function to sort the rows in descending order based on the values in the team column of the data frame.

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

Note: Before using the arrange() function, you will need to first install the dplyr package by using the following syntax:

install.packages('dplyr')

Once the dplyr package has been successfully installed, you can use the arrange() function without encountering any package errors.

Example: How to Use arrange() with String in dplyr

Suppose we have the following data frame that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'B', 'B', 'C', 'B', 'C', 'A', 'C'),
                 points=c(22, 39, 24, 18, 15, 10, 28, 23),
                 assists=c(3, 8, 8, 6, 10, 14, 8, 17))

#view data frame
df

  team points assists
1    A     22       3
2    B     39       8
3    B     24       8
4    C     18       6
5    B     15      10
6    C     10      14
7    A     28       8
8    C     23      17

Suppose that we would like to sort the rows of the data frame in descending order based on the values in the team column.

Suppose we attempt to use the following syntax to do so:

library(dplyr)

#arrange rows in descending order based on values in team column
df %>% arrange(desc('team'))

  team points assists
1    A     22       3
2    B     39       8
3    B     24       8
4    C     18       6
5    B     15      10
6    C     10      14
7    A     28       8
8    C     23      17

Notice that the arrange() function did not work and the rows of the data frame are still sorted in the original order.

This is because the desc() function within the arrange() function does not recognize strings as input for column names.

In order to pass a string as an input, we must use the following syntax instead:

library(dplyr)

#arrange rows in descending order based on values in team column
df %>% arrange(desc(!!sym('team')))

  team points assists
1    C     18       6
2    C     10      14
3    C     23      17
4    B     39       8
5    B     24       8
6    B     15      10
7    A     22       3
8    A     28       8

Notice that the rows are now successfully sorted in descending order based on the values in the team column.

That is, the rows are sorted in reverse alphabetical order and the desc() function was able to accept a string column name as input since we wrapped it with the syntax !!sym().

Note that you can also use the arrange() function by simply passing the column name by itself without quotes around it. This will also allow you to sort the rows based on one or more specific column names.

Note: You can find the complete documentation for the arrange() function in dplyr here.

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 *