The arrange() function with multiple columns

We can use the arrange() function on multiple columns as well. In this case the order of the columns in the function parameters, sets a hierarchy of ordering. The function starts by ordering the rows based on the first column defined in the parameters. In case there are several rows with the same value, the function decides the order based on the second column defined in the parameters. If there are still multiple rows with the same values, the function decides based on the third column defined in the parameters (if defined) and so on.

In the following example we use the pres_results_subset data frame, containing election results only for the states: "TX"(Texas),"UT"(Utah) and "FL"(Florida). First we sort the data frame in a descending order based on the year column. Then, we add a second level, and order the data frame based on the dem column:

arrange(pres_results_subset, year, dem)
# A tibble: 33 x 6
   year state total_votes   dem   rep   other
  <dbl> <chr>       <dbl> <dbl> <dbl>   <dbl>
1  1976 UT         541218 0.336 0.624 0.0392 
2  1976 TX        4071884 0.511 0.480 0.00817
3  1976 FL        3150631 0.519 0.466 0.0143 
# … with 30 more rows

As you can see in the output, the data frame is overall ordered based on the year column. However, when the value of year is the same, the order of the rows is decided by the dem column.

Sort data frames by columns