How to sort a list of lists in R?
Solution 1
Try this:
list.of.lists[order(sapply(list.of.lists,'[[',1))]
Solution 2
You have a lot of structure in your list.of.lists. Depending on other processing you need to do, you might want to make it into a two-dimensional list like so:
list.2d <- sapply(list.of.lists, cbind)
and, possibly, from there, into a data frame like this:
df <- data.frame(t(list.2d))
(Technically, a data frame is a type of list.) Sorting by a particular set of columns, and extracting subsets of elements can then be a bit more conventional. (Though I also really like the accepted answer here.)
Related videos on Youtube
mjnichol
Updated on June 18, 2022Comments
-
mjnichol about 2 years
I am attempting to sort a list of lists in R. Each sublist contains an integer and a character string. My goal is to order the lists such that the final list is sorted by the integers in ascending order. Below is an example of what I am trying to accomplish:
a <- list(-5,"help") b <- list(3, "stack") c <- list(1, "me") d <- list(10, "overflow") list.of.lists <- list(a,b,c,d) magic.sort(list.of.lists) # Below is not exactly how it would be displayed, but should be understandable -5, "help" 1, "me" 3, "stack" 10, "overflow"
Is there a nice way within R to achieve this result? Ideally the result should be returned as a list of lists as well.
-
mjnichol over 9 yearsThanks for the fast and correct answer! I tried to accept it, but it seems I need to wait a few minutes! While the clock ticks down on accepting this is there anyway you could elaborate on what each part is doing? I am most confused by your usage of sapply. What does the '[[' specify? Also, what is the last argument in sapply specifying? I am new to R...
-
thelatemail over 9 years@mjnichol - in long hand, you could write:
sapply(list.of.lists,function(x) x[[1]] )
- it is just extracting the first element from each list and then returning that as a vector which is then passed toorder
. E.g. -a[[1]]
and'[['(a,1)
are equivalent. -
mjnichol over 9 yearsAh, I sort of figured as much. Thank you!
-
emeralddove almost 7 yearsthank you. need to eliminate levels that contribute less than x% or retain levels that contribute more than say y%. any help much appreciated.
-
joran almost 7 years@makcbe It's pretty hard to help with such a limited description of your problem. I would suggest asking your own question, but be sure to include a good reproducible example that illustrates your situation.
-
Mark almost 4 yearsonly suggestion is to use
vapply
to ensure your sorting works as expected -
Pablo Adames over 3 yearsI think this answer brings forth the insight that data frames are implemented as lists of constant length vectors that among other things, have nice syntax to allow us to do what the question asked but with the added transformation into a data frame. Thanks!
-
Pablo Adames over 3 yearsI would also add that I'd prefer to call
list.2d
amatrix.of.lists
becausecbind
produces a 1 x 2 matrix with each of the lists in a cell of the single row. -
Timbus Calin over 2 years@thelatemail to me your solution seems more intuitive to me, I prefer to keep it this way (coming from a Python background)