in the` [1] 67504πŸ•΅πŸΏ 🍫 🀢🏼 Sorting a named list in R - sorting πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸ» πŸ‘ŠπŸ½ πŸ‘†πŸΎto the` [1] 36666πŸ•΅πŸΏ 🍫 🀢🏼 Sorting a named list in R - sorting πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸ» πŸ‘ŠπŸ½ πŸ‘†πŸΎof the` [1] 79665πŸ•΅πŸΏ 🍫 🀢🏼 Sorting a named list in R - sorting πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸ» πŸ‘ŠπŸ½ πŸ‘†πŸΎon the` [1]...">

Sort named list in R

I have a named list of thermal frequencies,

> list $`in the` [1] 67504 $`to the` [1] 36666 $`of the` [1] 79665 $`on the` [1] 31261 $`to be` [1] 25862 $`for the` [1] 28332 

I want to sort them in descending order according to frequencies. How to do it? I tried sort, sort.list, order , but had errors stating that they did not accept this type of list.

+6
source share
2 answers

You can try unlist and then use order

 lst[order(unlist(lst),decreasing=TRUE)] # $`4` #[1] 9 #$`3` #[1] 7 #$`1` #[1] 5 #$`2` #[1] 4 #$`5` #[1] 2 

data

 lst <- setNames(list(5,4,7,9,2),1:5) 
+7
source

If the list is large and includes large objects, would it be better to just use names?

  lst = lst[order(names(lst))] 
0
source

Source: https://habr.com/ru/post/979160/


All Articles