How to sort a list in R? Sorting or Ordering a list in R can be done by using lapply() function or using the order() function after converting to a vector using unlist().
In this article, I will explain how to order a list of elements by name or values in R and by applying ascending or descending order.
1. Quick Examples of Sorting List
Following are quick examples of sorting lists by name or values in R.
# Sort list in ascending order sorted_li <- lapply(li,sort) # Sort list in descending order sorted_li <- lapply(li,sort,decreasing=TRUE) # Sort named list sorted_li <- li[order(names(li))] # Unlist and use order() sorted_li <- li[order(unlist(li),decreasing=TRUE)]
2. R Sort List Values by Ascending
By using lapply() function you can sort the values of the list in R by ascending order, this function takes a list as an argument and the sort keyword. After applying the sort on the list it returns the ordered list. This by default sorts in ascending order, you can also force it by using
If you have characters in the list, you will see a resultant list in alphabetically sorted order.
# Create list li <- list(c(5,3,1,2,9,7)) # Sort list in ascending order sorted_li <- lapply(li,sort) sorted_li # Output #[] # 1 2 3 5 7 9
3. R Sort List in Descending
decreasing=TRUE to lapply() function to sort a list in descending order in R. If you have a character in the list, it sort Z to A (reverse alphabetical order).
# Sort list in descending order sorted_li <- lapply(li,sort,decreasing=TRUE) sorted_li # Output #[] # 9 7 5 3 2 1
4. Sort List by Name
To sort list by name in R, first, let’s create the named list and use the order() function to sort. Note that when you sort by name using order() it sorts names but not the values.
# Create named list li <- list(B = c(5,3,1,2,9,7), A = c(4,7,9,1,3,5), C = c(2,8,4,6,1,4)) li # Sort named list sorted_li <- li[order(names(li))] sorted_li
Yields below output.
4. Sort List of List
To sort the complex list of list data structure use order() and apply() functions together. In my example, each sublist contains an integer and a character string, I will order the lists such that the final list is sorted by the integers in ascending order.
# Sort list of list A <- list(1,"Java") B <- list(5, "Python") C <- list(6, "R") D <- list(2, "PHP") li <- list(A,B,C,D) li # Sort list of list sorted_li <- li[order(sapply(li,'[[',1))] sorted_li
I will leave this to you to run and explore the output.
6. Sort List by after unlist()
You can also sort lists by first unlist into vectors and using the order() function. The below example sorts the list in descending order.
# Unlist and use order() li <- setNames(list(5,3,1,2,9,7),1:6) sorted_li <- li[order(unlist(li),decreasing=TRUE)] sorted_li
In this article, you have learned how to sort or order a list in R by name, value, and by ascending, and descending order. Also learned how to sort list of list and by using unlist to vector.
- Sort Table in R with Examples
- How to Sort by Date in R?
- R Sort by Multiple Columns
- R Sort DataFrame Rows by Column Value
- Order DataFrame by one descending and one ascending column in R
- R Sort Vector
- Reorder Columns of DataFrame in R