从R中的任何级别删除列表中的空列表

时间:2016-03-04 18:41:25

标签: r list

我有一个列表,里面有几个列表:

> res$go
$MF
$MF[[1]]
                                 term evidence         id   pubmed
1 G-protein coupled receptor activity      IEA GO:0004930       NA
2                 calcium ion binding      IEA GO:0005509       NA
3                     protein binding      IPI GO:0005515 17474147

$MF[[2]]
NULL


$BP
$BP[[1]]
                                                             term evidence         id   pubmed
1                                                neuron migration      IEA GO:0001764       NA
2                           regulation of protein phosphorylation      IEA GO:0001932       NA
3 homophilic cell adhesion via plasma membrane adhesion molecules      IEA GO:0007156       NA
4                    G-protein coupled receptor signaling pathway      IEA GO:0007186       NA
5                                            axonal fasciculation      IEA GO:0007413       NA
6                              regulation of protein localization      IEA GO:0032880       NA
7                                                 cilium assembly      IEA GO:0042384       NA
8             Wnt signaling pathway, planar cell polarity pathway      NAS GO:0060071 24431302

$BP[[2]]
NULL


$CC
$CC[[1]]
                            term evidence         id
1                plasma membrane      IEA GO:0005886
2 integral component of membrane      IEA GO:0016021

$CC[[2]]
NULL

但是,其中一些内容中包含NULL列表。我试图找到一种从res$go列表中删除这些级别的空列表的方法,但我无法做到。

我试过了:

res$go[!is.null(res$go)]

但结果是一样的,即使我尝试进一步指定一个级别,我也无法删除它们。

res$go$BP[!is.null(res$go$BP)]

感谢您的帮助。

来自dput的输出

> dput(res$go)
structure(list(MF = list(structure(list(term = c("G-protein coupled receptor activity", 
"calcium ion binding", "protein binding"), evidence = c("IEA", 
"IEA", "IPI"), id = c("GO:0004930", "GO:0005509", "GO:0005515"
), pubmed = c(NA, NA, 17474147L)), .Names = c("term", "evidence", 
"id", "pubmed"), class = "data.frame", row.names = c(NA, 3L)), 
    NULL), BP = list(structure(list(term = c("neuron migration", 
"regulation of protein phosphorylation", "homophilic cell adhesion via plasma membrane adhesion molecules", 
"G-protein coupled receptor signaling pathway", "axonal fasciculation", 
"regulation of protein localization", "cilium assembly", "Wnt signaling pathway, planar cell polarity pathway"
), evidence = c("IEA", "IEA", "IEA", "IEA", "IEA", "IEA", "IEA", 
"NAS"), id = c("GO:0001764", "GO:0001932", "GO:0007156", "GO:0007186", 
"GO:0007413", "GO:0032880", "GO:0042384", "GO:0060071"), pubmed = c(NA, 
NA, NA, NA, NA, NA, NA, 24431302L)), .Names = c("term", "evidence", 
"id", "pubmed"), class = "data.frame", row.names = c(NA, 8L)), 
    NULL), CC = list(structure(list(term = c("plasma membrane", 
"integral component of membrane"), evidence = c("IEA", "IEA"), 
    id = c("GO:0005886", "GO:0016021")), .Names = c("term", "evidence", 
"id"), class = "data.frame", row.names = 1:2), NULL)), .Names = c("MF", 
"BP", "CC"))

1 个答案:

答案 0 :(得分:3)

如果使用递归数据结构,则工具箱的一个有用功能是walk

postwalk<-function(x,f,pred) if(pred(x)) f(lapply(x,postwalk,f,pred)) else f(x)

这比rapply更灵活。对于你的情况,

postwalk(data,function(x) Filter(Negate(is.null),x), function(x) class(x)=="list")

data是你的结构,应该有用。