[R] repeating matrices in a list

arun smartpink111 at yahoo.com
Fri Nov 30 18:57:59 CET 2012


Hi,
Try this:
lapply(1:3,function(x) g)

A.K.





----- Original Message -----
From: Anser Chen <anser.chen at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, November 30, 2012 12:50 PM
Subject: [R] repeating matrices in a list

Suppose I have the following  square, non-negative matrices

> g=matrix(c(0,2,4,0.25,0,0,0,0.6,0),3,3,byrow=T);



I want to create a list where this matrix is repeated multiple times. if I
do this brute force (manually), using

> env <- list(g,g,g)

works fine. Yields

[[1]]
     [,1] [,2] [,3]
[1,] 0.00  2.0    4
[2,] 0.25  0.0    0
[3,] 0.00  0.6    0

[[2]]
     [,1] [,2] [,3]
[1,] 0.00  2.0    4
[2,] 0.25  0.0    0
[3,] 0.00  0.6    0

[[3]]
     [,1] [,2] [,3]
[1,] 0.00  2.0    4
[2,] 0.25  0.0    0
[3,] 0.00  0.6    0



But - for a variety of purposes, I need to 'automate' building said list. I
tried using rep


>  env <- list(rep(g,each=3))

but this yields

[1] 0.00 0.00 0.00 0.25 0.25 0.25 0.00 0.00 0.00 2.00 2.00 2.00 0.00 0.00
0.00
[16] 0.60 0.60 0.60 4.00 4.00 4.00 0.00 0.00 0.00 0.00 0.00 0.00

Any suggestions/pointers to the obvious?

Thanks in advance...

    [[alternative HTML version deleted]]

______________________________________________
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