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
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 > >
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 >
Alexandre Sanchez Pla <asanchez <at> ub.edu> writes:
:
: 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.
:
Try this:
f <- function(x) if (is.null(x$VAL)) NA else x$VAL
unlist(lapply(tst.list, lapply, f))
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 > >