An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121130/1bca00f2/attachment.pl>
repeating matrices in a list
4 messages · Anser Chen, Sarah Goslee, arun
You are so close:
rep(list(g), 3)
[[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
Sarah
On Fri, Nov 30, 2012 at 12:50 PM, Anser Chen <anser.chen at gmail.com> wrote:
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...
-- Sarah Goslee http://www.functionaldiversity.org
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.
HI, You could also use: res1<-sapply(1:3,function(x) g,simplify=FALSE) #or res2<-replicate(3,g,simplify=FALSE) ?identical(res1,res2) #[1] TRUE 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.