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: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > Alexandre Sanchez Pla > Sent: Wednesday, January 26, 2005 8:50 AM > To: [email protected] > 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 > [EMAIL PROTECTED] > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
