[R] executing loop

Peter Langfelder peter.langfelder at gmail.com
Wed Sep 29 20:13:55 CEST 2010


for (j in 1:n)
{
   if (j%%2==0)
   {
     iRange = c(n:1)
    } else
      iRange = c(1:n)
    for (i in iRange)
    {
       your code
    }
}

Peter


On Wed, Sep 29, 2010 at 10:40 AM, cassie jones <cassiejones26 at gmail.com> wrote:
> Dear All,
>
>
> I am trying to define a loop for a m*n matrix, where i=1:n and j=1:m. Unlike
> the usual for loop, i should go in the following way:
>
> For j=1,
> i=1,2,3,....n
> For j=2,
> i=n,n-1,n-2,......,1
> For j=3,
> i=1,2,3,.....n etc.
> which means i should go in either increasing or decreasing order
> alternatively.


for (j in 1:n)
{
   if (j%%2==0)
   {
     iRange = c(n:1)
    } else
      iRange = c(1:n)
    for (i in iRange)
    {
       your code
    }
}

Peter



More information about the R-help mailing list