arrange()

arrange()

The arrange() function will arrange a data frame by a specific variable, in ascending order.


You can use the desc() argument with the arrange() function to arrange the data frame in descending order.

NOTE: The arrange() function is similar to the sort() function, however, arrange() works with data frames, while sort() works with vectors (see the sort() function).

Example 1: 

For instance, when we use the arrange() function to sort the Fingers data frame by Thumb, it will sort the values for Thumb in ascending order (smallest to largest) by default, like so:

# arrange Thumb in ascending order

arrange(FingersThumb)


Example of output from running the code above:
output of arranging Thumb in ascending order

Example 2:

But if we want the values to be sorted in descending order (largest to smallest) we can include the desc() function in the argument for arrange(), like so:

# arrange Thumb in descending order

arrange(Fingers, desc(Thumb))


Example of output from running the code above:
Example output from arranging Thumb in descending order


    • Related Articles

    • desc()

      The desc() function can be used with the arrange() function to arrange a variable in a data frame in descending order. Example 1:  For instance, when we use the arrange() function to sort the Fingers data frame by Thumb, it will sort the values for ...
    • sort()

      The sort() function will sort a vector (or a single column in a data frame) by a specific variable, in ascending order. You can use the decreasing = TRUE argument with the sort() function to sort the data frame in descending order (be careful not to ...