r - 从列表列表中选择子列表

标签 r list sorting subset

我想从列表列表中删除子列表。例如,假设我有一个像这样结构的大列表

mybiglist <- vector('list', 5)
names(mybiglist) <- paste0('item:', seq_along(mybiglist))
for(i in seq_along(mybiglist)){
  a <- rbinom(8, 5, i/10)
  b <- sum(c)

  tmp <- list(binomial = a, total = b)
  mybiglist[[i]] <- tmp
}

我想找到总数最高的子列表(整个项目:x)并将该列表保存为新对象。

实现这一目标的有效方法是什么?如果可能的话,我不想转换为不同的对象类型。

最佳答案

使用:

mybiglist[[which.max(sapply(mybiglist, `[[`, 'total'))]]

给出:

$binomial
[1] 4 2 2 5 5 1 3 2

$total
[1] 24

将其分配给新对象也会为您提供一个新列表:

> new <- mybiglist[[which.max(sapply(mybiglist, `[[`, 'total'))]]
> class(new)
[1] "list"

关于r - 从列表列表中选择子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46124074/

相关文章:

excel - R 使用链接写入 Excel

php - 从数组中获取三个最大值

c++ - 我如何根据另一个预先排序的字符串 vector 来排序包含字符串的 vector ?

android - 如何使用MVVM Android应用程序中的存储库模式和实时数据正确地对列表进行排序?

r - 将数据框拆分为列表中的多个数据框,每列分别

r - if/while(条件)出错 { : missing Value where TRUE/FALSE needed

r - 从 Excel 过渡到 R : how to implement if statement and index logic in R?

python - 列表中列表之间的公共(public)列表

一个numpy数组中元素的python位置与另一个数组中相等元素的位置

java - 错误。数据库查询同时添加到多个列表