r语言 - 任何从嵌套列表中更优雅地提取命名IntegerList的方法



我在IntegerList中有一个位置索引列表,我打算过滤给定的阈值,它工作得很好。但是,我想为每个IntegerList提取一个特定的过滤集以供进一步使用。我知道myList是嵌套列表,数据是基于真实数据集进行模拟的。有没有办法检索想要的IntegerList容易和优雅?我要怎么做才能取出来?

要运行迷你示例,需要以下库:

library(IRanges)
library(S4Vectors)

示例:

myList <- list(f1=IntegerList(1,2,3,4,1,1,1,integer(0),1,2,4),
               f2=IntegerList(1,5,integer(0),integer(0),2,3,4,6,1,5,6),
               f3=IntegerList(1,4,6,7,2,3,3,7,2,5,7))
len <- Reduce('+', lapply(myList, lengths))
keepMe <- len >= length(myList)

我打算按如下方式过滤它们:

res.filt <- lapply(myList, function(elm) {
  ans <- list(keep=elm[keepMe], droped=elm[!keepMe])
  ans
})

我的粗略输出:

Keep.list <- list(f1.kp=res.filt$f1$keep, f2.kp=res.filt$f2$keep, f3.kp=res.filt$f3$keep)
Drop.list <- list(f1.dp=res.filt$f1$droped, f2.dp=res.filt$f2$droped, f3.dp=res.filt$f3$droped)

基于我粗略的输出,我怎样才能得到更优雅的输出?有什么有效的方法来实现我的产出吗?有人能告诉我怎么做吗?或者有什么建议如何得到我期望的输出?提前感谢

你过滤矢量列表的思维过程/流程是合乎逻辑的,而且非常理想,但如果你使用purrr:

,你可以稍微收紧一下。
library(purrr)
map(myList, lengths) %>% 
  reduce(`+`) %>% 
  map_lgl(`>=`, length(myList)) -> keep_me
keep_list <- map(myList, ~.[keep_me])
drop_list <- map(myList, ~.[!keep_me])

最新更新