To sort a vector in ascending order in R, you can use the sort
function.
In this example,
vec
with several numeric values.sort
function to sort the vector vec
in ascending order. The sort
function sorts the elements of the vector in ascending order.# Step 1: Create a numeric vector
vec <- c(5, 2, 8, 3, 9, 1)
# Step 2: Sort the vector in ascending order
sorted_vec <- sort(vec)
# Step 3: Print the sorted vector
print(sorted_vec)
[1] 1 2 3 5 8 9
In this example,
vec
containing several string values.sort
function to sort the vector vec
in ascending order. The sort
function sorts the elements of the vector in ascending order based on lexicographic order.# Step 1: Create a string vector
vec <- c("apple", "banana", "orange", "grape", "kiwi")
# Step 2: Sort the vector in ascending order
sorted_vec <- sort(vec)
# Step 3: Print the sorted vector
print(sorted_vec)
[1] "apple" "banana" "grape" "kiwi" "orange"
In this tutorial, we learned How to Sort a Vector in Ascending Order in R language with well detailed examples.