dplyr arrange(): How to Sort Data in R
arrange() reorders the rows of a data frame. Sort by one column, break ties with a second, and flip the direction with desc(). It is the quickest way to find the biggest, smallest, first or last.
What does arrange() do?
arrange() changes the order of rows. It does not add, remove or change any values. You give it a data frame and one or more columns to sort by:
arrange(data, column)
Piped, as in the pipe operator lesson:
data |> arrange(column)
By default, rows are sorted from smallest to largest, or from A to Z. This lesson is part of our dplyr tutorial. It builds on dplyr filter (choosing rows) and dplyr mutate (creating columns).
Sample data
The same ten US cities again, with five columns. Populations are rounded Census figures in thousands, for practice. Notice the rows start in an arbitrary order: New York first, Austin last.
library(dplyr)
cities <- tibble(
city = c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix",
"Philadelphia", "San Antonio", "San Diego", "Dallas", "Austin"),
state = c("NY", "CA", "IL", "TX", "AZ", "PA", "TX", "CA", "TX", "TX"),
region = c("Northeast", "West", "Midwest", "South", "West",
"Northeast", "South", "West", "South", "South"),
population_2010 = c(8175, 3793, 2696, 2099, 1446, 1526, 1327, 1307, 1198, 790),
population_2020 = c(8804, 3899, 2746, 2305, 1608, 1604, 1435, 1387, 1304, 962)
)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 |
| Los Angeles | CA | West | 3793 | 3899 |
| Chicago | IL | Midwest | 2696 | 2746 |
| Houston | TX | South | 2099 | 2305 |
| Phoenix | AZ | West | 1446 | 1608 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| San Antonio | TX | South | 1327 | 1435 |
| San Diego | CA | West | 1307 | 1387 |
| Dallas | TX | South | 1198 | 1304 |
| Austin | TX | South | 790 | 962 |
Sort a column ascending
Which city is the smallest? Sort by the 2020 population:
cities |> arrange(population_2020)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Austin | TX | South | 790 | 962 |
| Dallas | TX | South | 1198 | 1304 |
| San Diego | CA | West | 1307 | 1387 |
| San Antonio | TX | South | 1327 | 1435 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| Phoenix | AZ | West | 1446 | 1608 |
| Houston | TX | South | 2099 | 2305 |
| Chicago | IL | Midwest | 2696 | 2746 |
| Los Angeles | CA | West | 3793 | 3899 |
| New York | NY | Northeast | 8175 | 8804 |
The rows now run from Austin (962) at the top to New York (8804) at the bottom. Smallest first is called ascending order, and it is the default.
Sort descending with desc()
To arrange in descending order, wrap the column in desc():
cities |> arrange(desc(population_2020))
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 |
| Los Angeles | CA | West | 3793 | 3899 |
| Chicago | IL | Midwest | 2696 | 2746 |
| Houston | TX | South | 2099 | 2305 |
| Phoenix | AZ | West | 1446 | 1608 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| San Antonio | TX | South | 1327 | 1435 |
| San Diego | CA | West | 1307 | 1387 |
| Dallas | TX | South | 1198 | 1304 |
| Austin | TX | South | 790 | 962 |
Now the biggest city comes first. You may also see arrange(-population_2020) with a minus sign. It works for numbers only, while desc() also works for text and dates, so prefer desc().
Sort text alphabetically
Sorting a text column puts it in alphabetical order:
cities |> arrange(city)
| city | state | population_2020 |
|---|---|---|
| Austin | TX | 962 |
| Chicago | IL | 2746 |
| Dallas | TX | 1304 |
| Houston | TX | 2305 |
| Los Angeles | CA | 3899 |
| New York | NY | 8804 |
| Philadelphia | PA | 1604 |
| Phoenix | AZ | 1608 |
| San Antonio | TX | 1435 |
| San Diego | CA | 1387 |
Use desc(city) to go from Z to A. One detail for later: since dplyr 1.1.0, text is sorted by plain character code by default, so all capital letters come before all lowercase ones ("Zebra" before "apple"). City names all start with capitals, so you will not notice it here. Mixed-case data can behave surprisingly. See the common mistakes table.
Sort by multiple columns
To sort by multiple columns in R, list them in order of importance. Here, sort by region first, and inside each region put the largest city first:
cities |> arrange(region, desc(population_2020))
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Chicago | IL | Midwest | 2696 | 2746 |
| New York | NY | Northeast | 8175 | 8804 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| Houston | TX | South | 2099 | 2305 |
| San Antonio | TX | South | 1327 | 1435 |
| Dallas | TX | South | 1198 | 1304 |
| Austin | TX | South | 790 | 962 |
| Los Angeles | CA | West | 3793 | 3899 |
| Phoenix | AZ | West | 1446 | 1608 |
| San Diego | CA | West | 1307 | 1387 |
The regions are alphabetical (Midwest, Northeast, South, West), and within each region the populations go from big to small. Each column can have its own direction: here region is ascending and population_2020 is descending.
Break ties
When two rows have the same value, arrange() keeps them in their original order. Sort by state alone and the four Texas cities stay in the order they appeared in the data:
cities |> arrange(state)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Phoenix | AZ | West | 1446 | 1608 |
| Los Angeles | CA | West | 3793 | 3899 |
| San Diego | CA | West | 1307 | 1387 |
| Chicago | IL | Midwest | 2696 | 2746 |
| New York | NY | Northeast | 8175 | 8804 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| Houston | TX | South | 2099 | 2305 |
| San Antonio | TX | South | 1327 | 1435 |
| Dallas | TX | South | 1198 | 1304 |
| Austin | TX | South | 790 | 962 |
To control the order inside a tie, add a second column. This sorts by state and then by population, smallest first:
cities |> arrange(state, population_2020)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Phoenix | AZ | West | 1446 | 1608 |
| San Diego | CA | West | 1307 | 1387 |
| Los Angeles | CA | West | 3793 | 3899 |
| Chicago | IL | Midwest | 2696 | 2746 |
| New York | NY | Northeast | 8175 | 8804 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| Austin | TX | South | 790 | 962 |
| Dallas | TX | South | 1198 | 1304 |
| San Antonio | TX | South | 1327 | 1435 |
| Houston | TX | South | 2099 | 2305 |
Now Texas runs Austin, Dallas, San Antonio, Houston, and California runs San Diego, then Los Angeles. The second column only matters when the first column has a tie.
Sort by a calculation
You can sort by an expression without creating a column first. Which cities grew fastest, as a ratio of 2020 to 2010 population?
cities |> arrange(desc(population_2020 / population_2010))
| city | population_2010 | population_2020 |
|---|---|---|
| Austin | 790 | 962 |
| Phoenix | 1446 | 1608 |
| Houston | 2099 | 2305 |
| Dallas | 1198 | 1304 |
| San Antonio | 1327 | 1435 |
| New York | 8175 | 8804 |
| San Diego | 1307 | 1387 |
| Philadelphia | 1526 | 1604 |
| Los Angeles | 3793 | 3899 |
| Chicago | 2696 | 2746 |
Austin is first, because its population grew by about 22%. Phoenix and Houston follow. If you also want to see the growth figure, create it with mutate() first and sort by the new column.
Sort with missing values (NA)
Missing values are always placed last, whichever direction you choose. Take the small made-up rent table from the filter lesson, where two cities have no rent value:
rent <- tibble(
city = c("New York", "Chicago", "Houston", "Phoenix", "Austin", "Dallas"),
median_rent = c(3400, 1900, NA, 1650, 2000, NA)
)
rent |> arrange(median_rent)
| city | median_rent |
|---|---|
| Phoenix | 1650 |
| Chicago | 1900 |
| Austin | 2000 |
| New York | 3400 |
| Houston | NA |
| Dallas | NA |
rent |> arrange(desc(median_rent))
| city | median_rent |
|---|---|
| New York | 3400 |
| Austin | 2000 |
| Chicago | 1900 |
| Phoenix | 1650 |
| Houston | NA |
| Dallas | NA |
In both results, Houston and Dallas stay at the bottom. To put the missing values first, sort by is.na() in descending order, then by the column itself:
rent |> arrange(desc(is.na(median_rent)), median_rent)
| city | median_rent |
|---|---|
| Houston | NA |
| Dallas | NA |
| Phoenix | 1650 |
| Chicago | 1900 |
| Austin | 2000 |
| New York | 3400 |
is.na() gives TRUE for missing rows. Descending order puts TRUE before FALSE, so those rows rise to the top.
Sort in a custom order
Alphabetical order is not always what you want. Suppose you need the regions in the order West, South, Midwest, Northeast. Turn the column into a factor with the levels in your chosen order, and sort by that:
cities |>
arrange(
factor(region, levels = c("West", "South", "Midwest", "Northeast")),
city
)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Los Angeles | CA | West | 3793 | 3899 |
| Phoenix | AZ | West | 1446 | 1608 |
| San Diego | CA | West | 1307 | 1387 |
| Austin | TX | South | 790 | 962 |
| Dallas | TX | South | 1198 | 1304 |
| Houston | TX | South | 2099 | 2305 |
| San Antonio | TX | South | 1327 | 1435 |
| Chicago | IL | Midwest | 2696 | 2746 |
| New York | NY | Northeast | 8175 | 8804 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
The first sort key follows your custom order, and city arranges the cities alphabetically within each region.
Sort within groups
On grouped data, arrange() ignores the groups by default and sorts the whole table. Add .by_group = TRUE to sort by the grouping columns first:
cities |>
group_by(region) |>
arrange(desc(population_2020), .by_group = TRUE)
This produces the same row order as arrange(region, desc(population_2020)) above, but the result stays grouped. If you are not working with grouped data, simply list the columns as in the multiple-columns example. Grouping is explained in dplyr group_by and summarise.
arrange() in a pipeline
Sorting is usually one step in a longer chain. This lists cities outside Texas, biggest first, with only the columns we need:
cities |>
filter(state != "TX") |>
arrange(desc(population_2020)) |>
select(city, state, population_2020)
| city | state | population_2020 |
|---|---|---|
| New York | NY | 8804 |
| Los Angeles | CA | 3899 |
| Chicago | IL | 2746 |
| Phoenix | AZ | 1608 |
| Philadelphia | PA | 1604 |
| San Diego | CA | 1387 |
To keep just the top few rows after sorting, add slice_head(n = 3). The dplyr slice lesson covers that, including the shortcut slice_max(), which sorts and slices in one step.
Remember that arrange() returns a sorted copy. To keep it, assign it:
cities_sorted <- cities |> arrange(desc(population_2020))
dplyr arrange vs base R
| Task | dplyr | Base R |
|---|---|---|
| Ascending | arrange(cities, population_2020) | cities[order(cities$population_2020), ] |
| Descending | arrange(cities, desc(population_2020)) | cities[order(cities$population_2020, decreasing = TRUE), ] |
| Two columns | arrange(cities, region, desc(population_2020)) | cities[order(cities$region, -cities$population_2020), ] |
| Sort one vector | x |> sort() (base) | sort(c(3, 1, 2)) |
In base R, order() returns row positions, and you use them to reorder the data frame yourself. sort() only works on a single vector, so it cannot sort a whole data frame. arrange() avoids the repeated cities$ and the trailing comma.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Forgetting desc() | You get smallest first when you wanted the biggest | Wrap the column: desc(population_2020) |
arrange(desc(region, population_2020)) | Error: desc() takes only one column | Use desc() on each column separately: arrange(desc(region), desc(population_2020)) |
| Columns in the wrong order | The first column is the main sort key. Swapping them changes the result | Put the most important column first |
| Expecting the sort to stick | The original data frame remains unsorted | Assign: cities <- cities |> arrange(...) |
| Numbers stored as text | "10" is placed before "9", because text sorts character by character | Convert first: mutate(x = as.numeric(x)) |
| Mixed-case text order | Capital letters come before lowercase ones (dplyr 1.1.0 and later) | Make the case consistent, or add .locale = "en" (needs the stringi package) |
Using sort() on a data frame | Error saying x must be atomic | Use arrange() or order() |
| Grouped data not sorted by group | arrange() ignores grouping by default | Add .by_group = TRUE |
Practice exercises
Use the cities and rent tables. Try each one before opening the answer.
Exercise 1
Sort the cities by their 2010 population, smallest first.
Show answer
cities |> arrange(population_2010)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Austin | TX | South | 790 | 962 |
| Dallas | TX | South | 1198 | 1304 |
| San Diego | CA | West | 1307 | 1387 |
| San Antonio | TX | South | 1327 | 1435 |
| Phoenix | AZ | West | 1446 | 1608 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| Houston | TX | South | 2099 | 2305 |
| Chicago | IL | Midwest | 2696 | 2746 |
| Los Angeles | CA | West | 3793 | 3899 |
| New York | NY | Northeast | 8175 | 8804 |
Exercise 2
Sort the cities by name from Z to A.
Show answer
cities |> arrange(desc(city))
| city | state | population_2020 |
|---|---|---|
| San Diego | CA | 1387 |
| San Antonio | TX | 1435 |
| Phoenix | AZ | 1608 |
| Philadelphia | PA | 1604 |
| New York | NY | 8804 |
| Los Angeles | CA | 3899 |
| Houston | TX | 2305 |
| Dallas | TX | 1304 |
| Chicago | IL | 2746 |
| Austin | TX | 962 |
Exercise 3
Sort by state from A to Z, and within each state put the biggest 2020 population first.
Show answer
cities |> arrange(state, desc(population_2020))
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| Phoenix | AZ | West | 1446 | 1608 |
| Los Angeles | CA | West | 3793 | 3899 |
| San Diego | CA | West | 1307 | 1387 |
| Chicago | IL | Midwest | 2696 | 2746 |
| New York | NY | Northeast | 8175 | 8804 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| Houston | TX | South | 2099 | 2305 |
| San Antonio | TX | South | 1327 | 1435 |
| Dallas | TX | South | 1198 | 1304 |
| Austin | TX | South | 790 | 962 |
Exercise 4
Find the three cities that gained the most people between 2010 and 2020. Show the city and the gain.
Show answer
cities |>
mutate(growth = population_2020 - population_2010) |>
arrange(desc(growth)) |>
slice_head(n = 3) |>
select(city, growth)
| city | growth |
|---|---|
| New York | 629 |
| Houston | 206 |
| Austin | 172 |
Exercise 5
Sort the rent table with the missing values first, then from the highest rent to the lowest.
Show answer
rent |> arrange(desc(is.na(median_rent)), desc(median_rent))
| city | median_rent |
|---|---|
| Houston | NA |
| Dallas | NA |
| New York | 3400 |
| Austin | 2000 |
| Chicago | 1900 |
| Phoenix | 1650 |
dplyr FAQ
How do I sort a data frame in R with dplyr?
Use arrange() with the column to sort by, for example arrange(cities, population_2020). The default order is ascending, from smallest to largest or A to Z.
How do I arrange in descending order in dplyr?
Wrap the column in desc(): arrange(cities, desc(population_2020)). It works for numbers, text and dates.
How do I sort by multiple columns in dplyr?
List the columns in order of importance, separated by commas: arrange(cities, region, desc(population_2020)). Later columns only break ties in earlier ones.
Where does arrange() put NA values?
Always at the end, whether you sort ascending or descending. To put them first, sort by desc(is.na(column)) before the column itself.
What is the difference between arrange(), order() and sort()?
arrange() sorts the rows of a data frame. order() is base R and returns the positions you use to reorder rows. sort() only works on a single vector, not on a data frame.
Why does my original data frame stay unsorted?
arrange() returns a new, sorted copy and never changes the original. Assign it with <- to keep the sorted version.
Key points
arrange()reorders rows. It never changes values or removes rows.- The default is ascending. Use
desc()for descending order. - List several columns to sort by more than one. The first is the main key, and later ones break ties.
- Missing values always go last. Use
desc(is.na(x))to bring them to the top. - You can sort by an expression, or by a factor to get a custom order.
- The result is a new table, so assign it with
<-to keep it.
Next, learn how to change column names with dplyr rename. To sort and take the top rows in one step, see dplyr slice.