# [R] saving estimates from a for loop for later use

Gabor Grothendieck ggrothendieck at gmail.com
Sat Apr 8 04:51:52 CEST 2006

```Another possibility is to create a data.frame.  I've just put
one statistic, sum, in it but you could put in as many as
you like:

> g <- expand.grid(i = 1:2, j = 1:2, k = 1:3)
> f <- function(x) with(x, c(i = i, j = j, k = k, sum = i+j+k))
> do.call("rbind", lapply(split(g, rownames(g)), f))
i j k sum
1  1 1 1   3
2  2 1 1   4
3  1 2 1   4
4  2 2 1   5
5  1 1 2   4
6  2 1 2   5
7  1 2 2   5
8  2 2 2   6
9  1 1 3   5
10 2 1 3   6
11 1 2 3   6
12 2 2 3   7

On 4/7/06, Brian Quinif <bquinif at gmail.com> wrote:
> Thanks for the suggestion, but I do not know how to get it to work for
> the actual matrix I want to create.  This is not reproducible, but
> here is the form of the matrix I want:
>
> #above I compute satt.yr.hrs and satt.full.load using Match()
>
> Estimates <- matrix(c(satt.yr.hrs\$est,
>
> In my real program, I have 3 loops with i in 1:3, j in 1:3 and m in
> 1:6.  I want to be able to combine the various versions of Estimates
> in many combinations to make tables.
>
> BQ
>
> 2006/4/7, Liaw, Andy <andy_liaw at merck.com>:
> > If the matrices you're storing inside the loop are all the same dimension
> > (as in your example), it's probably better to store them in an array; e.g.:
> >
> > i1 <- 2:3
> > i2 <- 11:12
> > x <- array(0, c(4, 1, length(i1), length(i2)))
> >
> > for (i in seq(along=i1)) {
> >    for (j in seq(along=i2)) {
> >        x[, , i, j] <- matrix(c(i1[i], i2[j], i1[i] + i2[j], i1[i]*i2[j]),
> > nrow=4)
> >    }
> > }
> > dimnames(x) <- list(NULL, NULL, i1, i2)
> > x[, , "2", "11", drop=FALSE]
> > x[, , "3", "11", drop=FALSE]
> >
> > (Notice you need the drop=FALSE to keep it a matrix, because it only has one
> > column.)
> >
> > Andy
> >
> > From: Brian Quinif
> > >
> > > I kind of understand this recommendation, but I can't figure
> > > out how to work with x once I have created it.  Here is some
> > > fully reproducible code.  It creates a 4x1 matrices within
> > > the loops.  Once done with the looping, I want to cbind these
> > > matrices together.  I know it's a simple question, but I
> > > can't figure it out.
> > >
> > > x <- list()
> > > for (i in 2:3)
> > > for (j in 11:12) {
> > > estimates <- matrix(c(i,j,i+j,i*j),nrow=4)
> > > x[[as.character(i)]][[as.character(j)]]  <- estimates }
> > >
> > >
> > > So, once I've done this, how can I reference, say, the
> > > "estimates" matrix created when i=2 and j=12?
> > >
> > > Thanks,
> > >
> > > BQ
> > >
> > > 2006/4/7, jim holtman <jholtman at gmail.com>:
> > > >
> > > > use a 'list';
> > > >
> > > > x <- list()
> > > > for (i in 1:3)
> > > >   for (j in 0:2) {
> > > >      .....your calculations.....
> > > >      x[[as.character(i)]][[as.character(j)]] <- yourResults
> > > > }
> > > >
> > > >
> > > >
> > > > On 4/7/06, Brian Quinif <bquinif at gmail.com> wrote:
> > > > >
> > > > Thanks to the help of many on this list, I am now an R user
> > > and have
> > > > been able to write some functioning code to do matching estimation.
> > > >
> > > > I have two for loops (i in 1:3, and j in 0:2).  Within the loops, I
> > > > had been creating matrices of relevant estimation
> > > coefficents in order
> > > > to make lots of LaTeX tables.
> > > >
> > > > Well, now I want to be able to combine the results of many
> > > different
> > > > estimations from within the loops into one larger table outside the
> > > > loops.
> > > >
> > > > How can I save the estimation results from each iteration of the
> > > > estimation within a loop for later use outside the loop?
> > > >
> > > > Thanks,
> > > >
> > > > BQ
> > > >
> > > > ______________________________________________
> > > > R-help at stat.math.ethz.ch mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > http://www.R-project.org/posting-guide.html
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Jim Holtman
> > > > Cincinnati, OH
> > > > +1 513 646 9390 (Cell)
> > > > +1 513 247 0281 (Home)
> > > >
> > > > What the problem you are trying to solve?
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > http://www.R-project.org/posting-guide.html
> > >
> > >
> >
> >
> > ------------------------------------------------------------------------------
> > Notice:  This e-mail message, together with any attachment...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help