在R中对命名列表进行排序 [英] Sort a named list in R

查看:109
本文介绍了在R中对命名列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个术语频率的命名列表,

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

$`for the`
[1] 28332

我想根据频率将它们降序排列.这该怎么做?我尝试了 sort,sort.list,order ,但是有错误提示他们不接受这种类型的列表.

I want to sort them into descending order according to the frequencies. How to do this? I tried sort, sort.list, order but had errors saying they don't accept this type of list.

推荐答案

您可以按unlist尝试,然后使用order

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

数据

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

这篇关于在R中对命名列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆