Using the order() function in R, sort data frame by column. order() function as default sorts the data in ascending order. However using the minus (-) sign before the column, we can sort data frame by column in descending order.
We can use the order() function to sort a data frame by a single column or multiple columns.
In this tutorial, we will discuss in R programming, how to sort data frame by column.
Sort Data Frame by Column
Use the order() function to sort the data frame by column. Specify the column name by which you want to sort data in a data frame.
Let’s consider an example to understand the sorting of a data frame.
# Create a data frame
student_info <- data.frame(
name = c("Tom","Kim","Sam","Julie","Emily","Chris"),
age = c(20,21,19,20,21,22),
gender = c('M','F','M','F','F','M'),
marks = c(72,77,65,80,85,87)
)
# Print the data frame
student_info
The output of the created data frame in R is:
name age gender marks
1 Tom 20 M 72
2 Kim 21 F 77
3 Sam 19 M 65
4 Julie 20 F 80
5 Emily 21 F 85
6 Chris 22 M 87
Let’s consider ordering data frame by age of the student, use the following code.
# Order data frame by column age
orderby_age <- student_info[order(student_info$age),]
# Print the sorted order data frame
orderby_age
The output of the above sorting of the data frame by the content of the column age is:
name age gender marks
3 Sam 19 M 65
1 Tom 20 M 72
4 Julie 20 F 80
2 Kim 21 F 77
5 Emily 21 F 85
6 Chris 22 M 87
Sorting Data Frame by Multiple Columns
Using the order() function in R, specify multiple columns to sort the data frame. By default, it will sort the data frame in ascending order.
Let’s understand the sorting of a data frame by multiple columns with the student_info data frame.
# Order data frame data by multiple columns age and marks
order_age_marks <- student_info[order(student_info$age,student_info$marks),]
print the sorted data frame
order_age_marks
name age gender marks
3 Sam 19 M 65
1 Tom 20 M 72
4 Julie 20 F 80
2 Kim 21 F 77
5 Emily 21 F 85
6 Chris 22 M 87
Sort Data Frame by Column Descending in R
To sort data frame by column descending in R can be achieved using the minus sign (-) before the column name in order() function.
In the above examples, we have seen, the order() function in R always sorts the data frame in ascending order.
Using the above student_info data frame to perform sorting of a data frame by column value in descending order.
# Sort data frame by column in descending order, use - sign
sort_by_marks <- student_info[order(-student_info$marks),]
# Print the sort data frame by column content in descending order
sort_by_marks
name age gender marks
6 Chris 22 M 87
5 Emily 21 F 85
4 Julie 20 F 80
2 Kim 21 F 77
1 Tom 20 M 72
3 Sam 19 M 65
Sort Data Frame by Ascending and Descending
The order() function R sorts the data in ascending order, use minus sign (-) to order data by descending order based on the contents of the column in a data frame.
Let’s consider the student_info data frame to sort data frame by ascending and descending order by the column value.
# Sort the data frame by ascending and descending
sort_data <- student_info[order(student_info$age,-student_info$marks),]
# Print the data frame
sort_data
In the above R code, order() sorts the data frame in ascending order for column age and descending order for column marks.
name age gender marks
3 Sam 19 M 65
4 Julie 20 F 80
1 Tom 20 M 72
5 Emily 21 F 85
2 Kim 21 F 77
6 Chris 22 M 87
Conclusion
I hope the above article on sorting data frame by column value in R is helpful to you.
Using the order() function, we can order the data frame in ascending or descending order.