[R] List of lists ?

David Winsemius dwinsemius at comcast.net
Mon Aug 9 19:16:37 CEST 2010


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



More information about the R-help mailing list