Sort a named list in R

后端 未结 2 1034
鱼传尺愫
鱼传尺愫 2021-01-01 19:04

I have a named list of term frequencies,

> list
$`in the`
[1] 67504

$`to the`
[1] 36666

$`of the`
[1] 79665

$`on the`
[1] 31261

$`to be`
[1] 25862

$`         


        
2条回答
  •  灰色年华
    2021-01-01 19:41

    You can try by 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)
    

提交回复
热议问题