Actually, what you want is sapply.

sapply(tst.list, "[[", "VAL")

Kevin

Alexandre Sanchez Pla wrote:
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



______________________________________________ [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

Reply via email to