desc()

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 Thumb in ascending order (smallest to largest) by default, like so:

# arrange Thumb in ascending order

arrange(Fingers, Thumb)


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

    • 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()

      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 ...
    • Data

      Data Data are recorded observations, measurements, or information collected to answer questions, test hypotheses, or make decisions. In statistics and data science, data are the raw material we analyze to discover patterns, relationships, and ...
    • Data Generating Process (DGP)

      Data Generating Process (DGP) The Data Generating Process (DGP) refers to the underlying mechanism—real or hypothetical—that produces the data we observe. A DGP specifies how variables are related, how randomness enters the system, and how observed ...
    • Statistical Model

      A statistical model is a simplified way of describing how data are generated. It helps us separate what we can explain using known information from what we cannot explain perfectly. We use statistical models for three main purposes: (1) to understand ...