To sort the items of a set in R, you can use the sort()
function.
In this example,
numbers
containing some integer elements using the c()
function.sort()
function and store the result in sorted_numbers
.numbers <- c(5, 3, 1, 4, 2)
sorted_numbers <- sort(numbers)
print('Sorted numbers:')
print(sorted_numbers)
[1] 1 2 3 4 5
In this example,
fruits
containing some string elements using the c()
function.sort()
function and store the result in sorted_fruits
.fruits <- c('Banana', 'Apple', 'Cherry', 'Date')
sorted_fruits <- sort(fruits)
print('Sorted fruits:')
print(sorted_fruits)
[1] 'Apple' 'Banana' 'Cherry' 'Date'
In this tutorial, we learned How to Sort Items of a Set in R language with well detailed examples.