[R] executing loop in circular way

jim holtman jholtman at gmail.com
Thu Sep 9 19:26:46 CEST 2010


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 <cassiejones26 at gmail.com> 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 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 can I define it?
>
> Thanks.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list