Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list by values preserving names in R

Tags:

sorting

r

Consider a list l like:

l<-list(a=24,b=12,c=30,d=1)

how to get the sorted version on values of such list, preserving names?

In result list the order of elements should be then: d,b,a and c corresponding to the sequence 1,12,24,30.

like image 532
licata1996 Avatar asked Nov 19 '25 14:11

licata1996


1 Answers

You can use order. Assuming that the length of each list element is 1 as showed in the example

l[order(unlist(l))]
like image 181
akrun Avatar answered Nov 22 '25 02:11

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!