Re: [R] executing loop

2010-09-29 Thread Peter Langfelder
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 wrote: > Dear All, > > > I am trying to define a loop for a m*n matrix, where i=1:n and j=1:m. Un

[R] executing loop

2010-09-29 Thread cassie jones
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 alternat

Re: [R] executing loop in circular way

2010-09-09 Thread jim holtman
try this: i <- 0 n <- 10 repeat{ cat(i + 1) # use the value; add 1 since we start at zero i <- (i + 1) %% n } On Thu, Sep 9, 2010 at 12:53 PM, cassie jones wrote: > Dear all, > > I am writing a program using for loop. The loop contains i which runs from > 1:n. There is a feature I ne

[R] executing loop in circular way

2010-09-09 Thread cassie jones
Dear all, I am writing a program using for loop. The loop contains i which runs from 1:n. There is a feature I need to include here. That is, when i=n, i+1 would be 1, not n+1. Basically the loop should run in a circular fashion. That also means, if i=1, i-1=n. Can anyone help me with this? How c