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 ...
    • Quantitative Variables

      A quantitative variable is a variable that measures a numerical amount or quantity. Quantitative variables answer questions like “how much?” or “how many?” Examples of Quantitative Variables Variable Example Values Height 62 inches, 70 inches Age 14 ...
    • Categorical Variable

      A categorical variable is a variable that places observations into groups or categories. Instead of measuring “how much,” categorical variables describe “which kind.” Examples of Categorical Variables Variable Example Categories Eye color blue, ...