[R] Fwd: List of lists ?

Carlos Petti carlos.petti at gmail.com
Tue Aug 10 15:12:38 CEST 2010


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


Perhaps a solution :

x <- list()
x[[2]] <- list()

x[[2]][[1]] <- c(1, 2, 3)
x[[2]][[2]] <- c(3, 2, 1)

2010/8/10 Carlos Petti <carlos.petti at gmail.com>:
> 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)
>
> 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
>>
>>
>



More information about the R-help mailing list