Yeah, that sounds inefficient to me also. I think you'd be better off using
multidimensional arrays instead of lists, since all your values are numeric.
See ?array.
Miguel
2010/3/15 Márcio Resende
>
> Hello R-helpers,
> I have the following code that works well,
>
> b <-list()
> for (i in 1:3)
> -Original Message-
> From: r-help-boun...@r-project.org
> [mailto:r-help-boun...@r-project.org] On Behalf Of Márcio Resende
> Sent: Monday, March 15, 2010 7:45 AM
> To: r-help@r-project.org
> Subject: [R] storing matrix(variables) in loop
>
>
> Hello R-help
Just in case...
b=array(NA,c(3,3,3,4))# that means b[matrix-row,matrix-col,i,j]
for (i in 1:3){
for (j in 1:4){
b[,,i,j]=matrix(runif(1),3,3)
}
}
b
(I think there are better ways to do this anyway...)
Miguel
[[alternative HTML version deleted]]
__
If you could describe exactly what is it that you're trying to
accomplish, we could be of better help (the reason I say this is
because the way you're trying to implement things is a bit
inefficient).
Anyways, you can't use two indices with a list.
One approach would be to nest lists, and you'd g
quick and dirty, use 'paste'
> b <- list()
> for (i in 1:3){
+ for (j in 1:4){
+ a <- matrix(runif(1),3,3)
+ b[[paste(i,j)]] <- a #but this doesn´t work
+ }
+ }
> b
$`1 1`
[,1] [,2] [,3]
[1,] 0.2059746 0.2059746 0.2059746
[2,] 0.2059746 0.2059746 0.2059746
[3,] 0.2059746 0.2059
Hello R-helpers,
I have the following code that works well,
b <-list()
for (i in 1:3){
a <- matrix(runif(1),3,3)
b[[i]] <- a
}
b
however, I need to do something similar with two loops and I was looking for
something that would look like
b <- list()
for (i in 1:3){
for (j in 1:4){
a <- matrix(r
6 matches
Mail list logo