2016-03-04 61 views
0

任何級別320交織名單我有內部都有幾個列表的列表:刪除空列表在R中

> 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")) 
+0

這不起作用。一切都是'FALSE',我期待NULL列表有一些'TRUE'。 – GabrielMontenegro

+0

@akrun檢查更新!塔克你! – GabrielMontenegro

+1

[this]的可能的重複(http://stackoverflow.com/questions/26539441/r-remove-null-elements-from-list-of-lists) – akrun

回答

2

有用的功能輸出你的工具箱,如果用遞歸數據結構的工作就是步行

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是你的結構,應該工作。