To sort a list in descending order in R, you can use the built-in sort
function with the decreasing = TRUE
argument.
In this example,
numbers
with numeric values.sort
function with the decreasing = TRUE
argument to sort the vector in descending order.sort
function modifies the original vector by sorting it in descending order.numbers <- c(5, 2, 8, 1, 3)
sorted_numbers <- sort(numbers, decreasing = TRUE)
print(sorted_numbers)
[1] 8 5 3 2 1
In this example,
words
with string values.sort
function with the decreasing = TRUE
argument to sort the vector in descending order.sort
function modifies the original vector by sorting it in descending order based on character values.words <- c('banana', 'apple', 'cherry')
sorted_words <- sort(words, decreasing = TRUE)
print(sorted_words)
[1] "cherry" "banana" "apple"
In this tutorial, we learned How to Sort a List in Descending Order in R language with well detailed examples.