[R] Indexing a loop-created list
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@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.
Re: [R] Indexing a loop-created list
__ R-help@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.
Re: [R] Indexing a loop-created list
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 : [EMAIL PROTECTED] 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@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. __ R-help@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.
Re: [R] Indexing a loop-created list
m - 2 n - 3 matlist - matrix(vector(list), m, n) for (i in 1:m) for (j in 1:n) matlist[[i,j]] - matrix(sample(12), 3, 4) matlist[[1,2]] ## access with double[[ subscripts __ R-help@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.
Re: [R] Indexing a loop-created list
Romain Lorrilliere wrote: 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))) } } Why it does not work as expected in this case? my_list - NULL for (i in 1:10) my_list - c(my_list, function(x) x^i) f - my_list[[2]] f(10) [1] 1e+10 Alberto Monteiro __ R-help@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.