You can do this...

# Some random data:
b_1 <- b_2 <- b_3 <- matrix(,2,3)
for(i in 1:3) eval(substitute(A <- matrix(rnorm(6), 2), list(A=paste('a', i,
sep=''))))


# the loop
for (i in 1:2) {
    for (j in 1:3) {
        for(k in 1:3) {
            eval(substitute(A[i,j] <- rank(c(a1[i,j],a2[i,j],a3[i,j]))[k],
list(A=as.symbol(paste('b_', k, sep='')))))
        }
    }
}



# For my own interest, I would have thought that:

thingy <- expression(A[i,j] <- rank(c(a1[i,j],a2[i,j],a3[i,j]))[k])
for (i in 1:2) {
    for (j in 1:3) {
        for(k in 1:3) {
            eval(do.call('substitute', list(expr=thingy,
env=list(A=as.symbol(paste('b_', k, sep=''))))))
        }
    }
}


# would have also worked, but it does not. Can someone please explain why
not?


Simon Knapp

On Tue, Jan 19, 2010 at 1:41 PM, David Winsemius <dwinsem...@comcast.net>wrote:

>
> On Jan 18, 2010, at 9:21 PM, rusers.sh wrote:
>
> > If the number of datasets for a* is small (here is 3), it is ok for
> > creating b_ijn[i, j, nn] and make assignments to it. But it will be
> > a little bit impossible for a larger number of datasets for a*, say
> > 999. We may need 999 lines to do this. Maybe there are other
> > alternatives.
>
> Read more carefully. The b_ijn[ , , ] array can be pre-dimensioned to
> any size. You must know the size since you are specifying a loop range.
>
>
> >
> > 2010/1/18 David Winsemius <dwinsem...@comcast.net>
> >
> > On Jan 18, 2010, at 7:19 PM, rusers.sh wrote:
> >
> > Hi,
> >  See example.
> >  for (i in 1:2) {
> >  for (j in 1:3) {
> >    b_1[i,j]<-rank(c(a1[i,j],a2[i,j],a3[i,j]))[1]
> >   b_2[i,j]<-rank(c(a1[i,j],a2[i,j],a3[i,j]))[2]
> >    b_3[i,j]<-rank(c(a1[i,j],a2[i,j],a3[i,j]))[3]
> >  }
> > }
> >  The inner codes is really repeated, so i want to change the inner
> > codes
> > into loops. Take nn is from 1 to 3,
> > something like,
> >  for (nn in 1:3) {
> > b_nn[i,j]<-rank(c(a1[i,j]:a3[i,j]))[nn]
> > }
> >  Anybody can tell me the correct method to specify the above codes?
> >
> > There is no correct method. You cannot index on the object name b_nn
> > that way. R has not been developing using a syntax with that much
> > flexibility.  If you want a 3D array of values, then you could
> > create b_ijn[i, j, nn] and make assignments to it. But if you tried
> > to do this with paste and assign, you will spending considerably
> > more time degbugging it than it is worth and it would likely be more
> > inefficient than what you have.
> >
> > --
> > David.
> >
> > Thanks.
> >
> > --
> > -----------------
> > Jane Chang
> > Queen's
> >
> >        [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help@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.
> >
> > David Winsemius, MD
> > Heritage Laboratories
> > West Hartford, CT
> >
> >
> >
> >
> > --
> > -----------------
> > Jane Chang
> > Queen's
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to