[R] Fwd: List of lists ?

William Dunlap wdunlap at tibco.com
Tue Aug 10 17:35:01 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Carlos Petti
> Sent: Tuesday, August 10, 2010 6:12 AM
> To: r-help at r-project.org
> Subject: [R] Fwd: List of lists ?
> 
> ---------- Forwarded message ----------
> From: Carlos Petti <carlos.petti at gmail.com>
> Date: 2010/8/10
> Subject: Re: [R] List of lists ?
> To: David Winsemius <dwinsemius at comcast.net>
> 
> 
> Thanks for answer.
> 
> I read the error messages but I did not find the solution :-(
> 
> Your solution works.
> But, a new problem remains because I want
> to use the list of lists as follows :
> 
> x <- list(list())
> 
> x[[2]][[1]] <- c(1, 2, 3)
> x[[2]][[2]] <- c(3, 2, 1)

You need to tell it that x[[i]] will be a list
for each i.  You can do that with
   x <- list()
   for(i in 1:3) {
       x[[i]] <- list()
       for(j in seq_len(i)) {
           x[[i]][[j]] <- i*100 + seq_len(j)
       }
   }
It might be easier to read and perhaps faster to do
   x <- list()
   for(i in 1:3) {
       xi <- list() # will become x[[i]]
       for(j in seq_len(i)) {
           xi[[j]] <- i*100 + seq_len(j)
       }
       x[[i]] <- xi
   }
If you know the ultimate length of a list, and it will
be long, it may save time to allocate the whole thing
up front with
   xlength <- 3
   x <- vector("list", xlength)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> Thanks in advance,
> Carlos
> 
> 2010/8/9 David Winsemius <dwinsemius at comcast.net>:
> >
> > On Aug 9, 2010, at 12:57 PM, Carlos Petti wrote:
> >
> >> Dear list,
> >>
> >> I have to use a list of lists containing vectors.
> >>
> >> For instance :
> >>
> >> [[1]]
> >> [[1]][[1]]
> >> [1] 1 2 3
> >>
> >> [[1]][[2]]
> >> [1] 3 2 1
> >>
> >> I want to attribute vectors to the main list
> >>
> >> without use of an intermediate list,
> >>
> >> but it does not work :
> >
> > More specifically it produces an error that has information in it.
> >> x[[1]][[1]] <- c(1, 2, 3)
> > Error in `*tmp*`[[1]] : subscript out of bounds
> >
> >>
> >> x <- list()
> >> x[[1]][[1]] <- c(1, 2, 3)
> >> x[[1]][[2]] <- c(3, 2, 1)
> >
> > So thinking perhaps we just needed another level of 
> subscripting "available"
> > I tried:
> >
> >> x <- list(list())
> >> x[[1]][[1]] <- c(1, 2, 3)
> >> x[[1]][[2]] <- c(3, 2, 1)
> >> x
> > [[1]]
> > [[1]][[1]]
> > [1] 1 2 3
> >
> > [[1]][[2]]
> > [1] 3 2 1
> >
> > Success. Moral: Read the error messages for meaning or at 
> least clues.
> > (Further testing showed that almost anything inside the 
> original list()
> > call, even NULL,  would have created enough structure for 
> the interpreter to
> > work with.
> >
> >>
> >
> > David Winsemius, MD
> > West Hartford, CT
> >
> >
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list