Sorting of Arrays in details
A vector is a one-dimensional array, defined by a single length dimension. A vector can be created using the c() function by passing a list of values. Sorting can be done in either ascending or descending order. Before sorting, certain factors should be considered:
- Sorting order – Ascending or Descending.
- Sorting based on multiple criteria – If sorting involves multiple columns, specify the order.
- Handling missing and duplicate values – Decide whether to remove or replace them, considering the impact on the data.
Method 1: sort() function
The sort() function in R is used to sort a vector. By default, it sorts in increasing order. To sort in descending order, set the decreasing parameter to TRUE.
Syntax:
sort(vector_name, decreasing = TRUE)
Parameters:
vector_name: The vector to be sorted.decreasing: A Boolean value that determines whether sorting should be in descending order.
Example 1: Sorting in Ascending Order
# Create a vector
numbers <- c(45, 12, 78, 23, 56, 89, 34)
# Sort in ascending order
sort(numbers)
Output:
[1] 12 23 34 45 56 78 89
Example 2: Sorting in Descending Order
# Sort in descending order
sort(numbers, decreasing = TRUE)
Output:
[1] 89 78 56 45 34 23 12
Method 2: order() function
To sort data frames, the order() function is used. It sorts the data based on the specified column. To sort in descending order, use a negative sign. Sorting can also be done with multiple criteria. If two values in a column are the same, a secondary column can be used for sorting (e.g., sorting names alphabetically when ages are the same).
Example: Sorting a Data Frame by Age
# Define a data frame
students <- data.frame("Age" = c(20, 18, 22, 25, 19),
"Name" = c("Aria", "Leo", "Sophia", "Daniel", "Mia"))
# Sort the data frame based on the Age column
sorted_students <- students[order(students$Age), ]
# Print the sorted data frame
print(sorted_students)
Output:
Age Name
2 18 Leo
5 19 Mia
1 20 Aria
3 22 Sophia
4 25 Daniel
Example 1: Sorting a Vector in Decreasing Order
# Define vector
numbers <- c(35, 10, 50, 25, 5, 40)
# Sort in decreasing order and return indices
order(-numbers)
Output:
[1] 3 6 1 4 2 5
Example 2: Sorting a Data Frame by Multiple Columns
# Define dataframe
students <- data.frame("Age" = c(14, 18, 14, 21, 18, 14),
"Name" = c("Liam", "Emma", "Noah",
"Olivia", "Ava", "Sophia"))
# Sort the dataframe first by Age, then by Name
sorted_students <- students[order(students$Age, students$Name), ]
# Print sorted dataframe
print(sorted_students)
Output:
Age Name
6 14 Sophia
3 14 Noah
1 14 Liam
5 18 Ava
2 18 Emma
4 21 Olivia
Method 3: Sorting an Array Using a Loop
# Create linear array
arr <- c(8, 3, 7, 2, 6, 5, 4, 1)
# Repeat until the array is sorted
repeat
{
swapped <- FALSE
# Iterate through the array
for (i in 2:length(arr))
{
newArr <- arr
if (arr[i - 1] > arr[i])
{
newArr[i - 1] <- arr[i]
newArr[i] <- arr[i - 1]
arr <- newArr
swapped <- TRUE
}
}
if (!swapped) {break}
}
# Print sorted array
print(arr)
Output:
[1] 1 2 3 4 5 6 7 8
Method 4: Using dplyr Package for Sorting
# Install and load dplyr package
install.packages("dplyr")
library(dplyr)
# Create dataframe
employees <- data.frame("Age" = c(30, 45, 28, 35, 40),
"Name" = c("David", "Alice", "Ethan",
"Olivia", "Sophia"))
# Sort the dataframe by Age using arrange()
sorted_employees <- arrange(employees, Age)
# Print sorted dataframe
print(sorted_employees)
Output:
Age Name
3 28 Ethan
1 30 David
4 35 Olivia
5 40 Sophia
2 45 Alice
Leave a Reply