Table of Contents
To sort a data frame in R by multiple columns simultaneously, you can use the “order” function. This allows you to specify the columns you want to sort by and the order in which you want them to be sorted. For example, if you have a data frame with columns for “Name”, “Age”, and “Salary”, you can sort the data frame by “Name” in alphabetical order and then by “Age” in descending order by using the code: order(df$Name, -df$Age). This will arrange the rows in the data frame according to the specified columns. Another example would be to sort by multiple columns at once, such as sorting by “Age” in ascending order and then by “Salary” in descending order by using the code: order(df$Age, -df$Salary). This will sort the data frame by age first and then within each age group, it will sort by salary in descending order.
Sort by Multiple Columns in R (With Examples)
You can use one of the following methods to sort a data frame by multiple columns in R:
Method 1: Use Base R
df[order(-df$column1, df$column2), ]
Method 2: Use dplyr
library(dplyr) df %>% arrange(desc(column1), column2)
The following examples show how to use each method in practice with the following data frame:
#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G'),
points=c(90, 90, 93, 91, 91, 99, 85),
assists=c(33, 28, 31, 39, 34, 40, 44))
#view data frame
df
team points assists
1 A 90 33
2 B 90 28
3 C 93 31
4 D 91 39
5 E 91 34
6 F 99 40
7 G 85 44
Method 1: Use Base R
The following code shows how to sort the data frame in base R by points descending (largest to smallest), then by assists ascending:
#sort by points descending, then by assists ascending df[order(-df$points, df$assists), ] team points assists 6 F 99 40 3 C 93 31 5 E 91 34 4 D 91 39 2 B 90 28 1 A 90 33 7 G 85 44
Notice that the rows of the data frame are ordered by points from largest to smallest, then by assists from smallest to largest.
Method 2: Use dplyr
The following code shows how to use functions from the package to sort the data frame by points descending (largest to smallest), then by assists ascending:
library(dplyr) df %>% arrange(desc(points), assists) team points assists 1 F 99 40 2 C 93 31 3 E 91 34 4 D 91 39 5 B 90 28 6 A 90 33 7 G 85 44
Once again, the rows of the data frame are ordered by points from largest to smallest, then by assists from smallest to largest.
Note: You can find the complete documentation for the arrange() function .
Additional Resources
Cite this article
stats writer (2024). How can I sort a data frame in R by multiple columns simultaneously?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-sort-a-data-frame-in-r-by-multiple-columns-simultaneously-can-you-provide-some-examples/
stats writer. "How can I sort a data frame in R by multiple columns simultaneously?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-sort-a-data-frame-in-r-by-multiple-columns-simultaneously-can-you-provide-some-examples/.
stats writer. "How can I sort a data frame in R by multiple columns simultaneously?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-sort-a-data-frame-in-r-by-multiple-columns-simultaneously-can-you-provide-some-examples/.
stats writer (2024) 'How can I sort a data frame in R by multiple columns simultaneously?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-sort-a-data-frame-in-r-by-multiple-columns-simultaneously-can-you-provide-some-examples/.
[1] stats writer, "How can I sort a data frame in R by multiple columns simultaneously?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I sort a data frame in R by multiple columns simultaneously?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
