[R] apply for nested lists

Liaw, Andy andy_liaw at merck.com
Wed Jan 26 18:26:39 CET 2005


Here's one (not so general) way:

> x <- unlist(tst.list)
> x[grep("VAL", names(x))]
1.1A.VAL 1.1B.VAL 1.1C.VAL 3.3A.VAL 3.3B.VAL 
   "172"    "134"      "0"     "33"      "2" 

Do you need the NA?

Andy

> From: Alexandre Sanchez Pla
> 
> Hi,
> 
> I am working with lists whose terms are lists whose terms are 
> lists. Although 
> the real ones contain locuslink identifiers and GO 
> annotations (I work with the 
> Bioconductor GO) package, I have prepared an simplified 
> example of what I have 
> and what I would like to do with it:
> 
> Imagine I have a list such as:
> 
> tst.list<-list("1"=list("1A"=list(ID="1A",VAL=172),"1B"=list
> (ID="1B",VAL=134),"1C"=list(ID="1C",VAL=0)),"2"=list("2A"=NA),
> "3"=list("3A"=list
> (ID="3A",VAL=33),"3B"=list(ID="3B",VAL=2)))
> 
> I would like, for instance, to be able to extract some values 
> such as the 
> content of the "VAL" field, which may sometimes not be available.
> I may do it using a nested for such as:
> 
> x<-character(0)
> for (i in 1:length(tst.list)){
>     if (!is.na(tst.list[[i]][[1]][[1]])){
>         for (j in 1:length(tst.list[[i]]))
>             {x<-c(x,tst.list[[i]][[j]]$VAL)}}
>     else    
>             {x<-c(x, NA)}}
> 
> which gives me what I need
> 
> > x
> [1] "172" "134" "0"   NA  "33"  "2"  
> 
> According to most R documents this may be done more 
> efficiently using apply 
> instructions, but I have failed in my temptatives to obtain the same
> 
> Thanks for any help.
> 
> Alex
> 
> 
> -- 
> Dr.Alex Sánchez
> Departament d'Estadística.
> Universitat de Barcelona
> asanchez at ub.edu
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list