[R] list of matrices

R. Michael Weylandt michael.weylandt at gmail.com
Wed Mar 21 17:21:10 CET 2012


You probably want

mylist[[i]] = function(...)

in your loop.

Similarly, when you want them again later, you need to use double
square brackets.

I once heard a useful metaphor for understanding the difference
between [ and [[ when it comes to lists.

If x (a list) is a train, then x[2] is the second car of that train,
while x[[2]] are the contents of that second car.

That's why things like, x[1:3] are well defined (the sublist
containing elements 1 through 3) while x[[1:3]] are not.

Hope this helps,
Michael

On Wed, Mar 21, 2012 at 11:08 AM, David Zastrau <davidz at tzi.de> wrote:
> Hello dear R-users,
>
> I am currently trying to fill some datasatructure (array, list, hash...)
> with matrices that are calculated by a function and do vary in size:
>
> mylist = list()
> for(i in 1:n)
>    mylist[i] = function(...) # returns a matrix
>
> print(mylist[1]) # prints only the first element of the matrix
>
>
> How can I store and afterwards access the matrices that are calculated by my
> function?
>
> Any help would be greatly appreciated!
>
> Best Wishes
> David
>
> ______________________________________________
> 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