[R] Indexing a loop-created list

Romain Lorrilliere romain.lorrilliere at ese.u-psud.fr
Thu Oct 19 17:07:26 CEST 2006


>  hi,
>
> try this:
>
> my_list <- NULL
>
> for (i in 1:n)
> 	{for (j in (i+1):m)
> 		{my_list <- c(my_list,list(i_create_a_matrix(i,j)))
> 	}
> }
>
>   
>
> Best,
>
> Romain
>
>
> Romain Lorrillière
>
>  
>
> UMR 8079 Laboratoire Ecologie, Systématique et Evolution
>
> Bât. 362
>
> Université Paris-Sud
>
> 91405 Orsay cedex
>
> France
>
>  
>
> tel : 01 69 15 56 85
>
> fax : 01 69 15 56 96
>
> mobile : 06 81 70 90 70
>
>  
>
> email : romain.lorrilliere at ese.u-psud.fr
>
>  
>
>
>
> Nicolas Prune a écrit :
>> Hello,
>>
>> I create a certain number (n*m) matrices, that I would like to store. These
>> matrices have different dimensions. As far as I know, for matrices of different
>> dimensions, there's no alternative to a list.
>>
>> Here's how I store them on the fly :
>>
>> my_list <- NULL
>>
>> for (i in 1:n)
>> 	{for (j in (i+1):m)
>> 		{my_list <- list(my_list,i_create_a_matrix(i,j)))
>> 	}
>> }
>>
>> But the indexation of the result is horrible ! (see below a simplified version,
>> with no matrices created but i*10+j added to the list instead). Is there a
>> cleaner way to get the same result ?
>>
>> Any help will be very welcomed.
>> Nicolas
>>
>> [[1]]
>> [[1]][[1]]
>> [[1]][[1]][[1]]
>> [[1]][[1]][[1]][[1]]
>> [[1]][[1]][[1]][[1]][[1]]
>> [[1]][[1]][[1]][[1]][[1]][[1]]
>> NULL
>>
>> [[1]][[1]][[1]][[1]][[1]][[2]]
>> [1] 12
>>
>>
>> [[1]][[1]][[1]][[1]][[2]]
>> [1] 13
>>
>>
>> [[1]][[1]][[1]][[2]]
>> [1] 14
>>
>>
>> [[1]][[1]][[2]]
>> [1] 23
>>
>>
>> [[1]][[2]]
>> [1] 24
>>
>>
>> [[2]]
>> [1] 34
>>
>> ______________________________________________
>> 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
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>



More information about the R-help mailing list