[R] apply for nested lists
Berton Gunter
gunter.berton at gene.com
Wed Jan 26 19:07:37 CET 2005
apply() statements **are** disguised loops and therefore are **not**
necessarily more efficient than explicit looping. Their principal advantage
is usually code readability.
As another readability issue, note that x[[i]][[j]][[k]] can be abbreviated
to x[[c(i,j,k]].
I leave to others the pleasure of deciphering and improving your code,
however.
-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
"The business of the statistician is to catalyze the scientific learning
process." - George E. P. Box
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
> Alexandre Sanchez Pla
> Sent: Wednesday, January 26, 2005 8:50 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] apply for nested lists
>
> 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