I would like to have a list where each element is a matrix, for example: my.list <- list(matrix(0, ncol=3, nrow=3), matrix(0, ncol=3, nrow=3), matrix(0, ncol=3, nrow=3)) The problem is, I would like to be able to change automatically the number of elements in the list (not only three as in the above example). That is, the instruction creating the list would be a part of a function that has an inputing parameter stating how many elements (matrices) the list has. I have tried several things, but none worked. However, this seems to be a rather simple problem. All help is welcome! LBA
Hi, What about: my.list <- vector(mode="list", length=number_of_matrices) ? HTH, Ivan Le 6/9/2010 15:46, Luis Borda de Agua a ?crit :> I would like to have a list where each element is a matrix, for example: > > my.list<- list(matrix(0, ncol=3, nrow=3), > matrix(0, ncol=3, nrow=3), > matrix(0, ncol=3, nrow=3)) > > The problem is, I would like to be able to change automatically the number of elements in the list (not only three as in the above example). > > That is, the instruction creating the list would be a part of a function that has an inputing parameter stating how many elements (matrices) the list has. > > I have tried several things, but none worked. > However, this seems to be a rather simple problem. > All help is welcome! > > LBA > > ______________________________________________ > 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. > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
I might give this a try as well: my.list <- replicate(3, matrix(0, ncol=3, nrow=3), simplify=FALSE) ---- args(replicate) function (n, expr, simplify = TRUE)> replicate(3, matrix(0, ncol=3, nrow=3), simplify=FALSE)[[1]] [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0 [[2]] [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0 [[3]] [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0> is.list(replicate(3, matrix(0, ncol=3, nrow=3), simplify=FALSE))[1] TRUE ---- Bill On Jun 9, 2010, at 9:54 AM, Ivan Calandra wrote:> Hi, > > What about: > my.list <- vector(mode="list", length=number_of_matrices) > ? > > HTH, > Ivan > > Le 6/9/2010 15:46, Luis Borda de Agua a ?crit : >> I would like to have a list where each element is a matrix, for example: >> >> my.list<- list(matrix(0, ncol=3, nrow=3), >> matrix(0, ncol=3, nrow=3), >> matrix(0, ncol=3, nrow=3)) >> >> The problem is, I would like to be able to change automatically the number of elements in the list (not only three as in the above example). >> >> That is, the instruction creating the list would be a part of a function that has an inputing parameter stating how many elements (matrices) the list has. >> >> I have tried several things, but none worked. >> However, this seems to be a rather simple problem. >> All help is welcome! >> >> LBA >> >> ______________________________________________ >> 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. >> >> > > -- > Ivan CALANDRA > PhD Student > University of Hamburg > Biozentrum Grindel und Zoologisches Museum > Abt. S?ugetiere > Martin-Luther-King-Platz 3 > D-20146 Hamburg, GERMANY > +49(0)40 42838 6231 > ivan.calandra at uni-hamburg.de > > ********** > http://www.for771.uni-bonn.de > http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php > > ______________________________________________ > 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.
Hi one option is to create an empty list with my.list <- vector("list", n) where n is number of elements in list. Then you can populate each element by my.list[[x]] <- something where x can be number or my.list[x] <-something where x is numeric vector Regards Petr r-help-bounces at r-project.org napsal dne 09.06.2010 15:46:09:> I would like to have a list where each element is a matrix, for example: > > my.list <- list(matrix(0, ncol=3, nrow=3), > matrix(0, ncol=3, nrow=3), > matrix(0, ncol=3, nrow=3)) > > The problem is, I would like to be able to change automatically thenumber of> elements in the list (not only three as in the above example). > > That is, the instruction creating the list would be a part of a functionthat> has an inputing parameter stating how many elements (matrices) the listhas.> > I have tried several things, but none worked. > However, this seems to be a rather simple problem. > All help is welcome! > > LBA > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.