Is this what you want? You will have to adjust the indices appropriately:
> x<-c(2,0,0,2,2,2)
> i <- seq_along(x)
> x[i]
[1] 2 0 0 2 2 2> # to "loop around", change how you index
> x[(i - 1) %% length(x) + 1]
[1] 2 0 0 2 2 2> i <- i + 1
> x[(i - 1) %% length(x) + 1]
[1] 0 0 2 2 2 2> i <- i + 1
> x[(i - 1) %% length(x) + 1]
[1] 0 2 2 2 2 0> i <- i + 1
> x[(i - 1) %% length(x) + 1]
[1] 2 2 2 2 0 0>
>
>
On Wed, Dec 21, 2011 at 6:56 AM, djrwicks <djrwicks at gmail.com>
wrote:> Given the following, how to I get x[i+1] to not return an NA result when it
> gets to the end of list x by looping back to the start of the list
> i.e should return: 0 0 2 2 2 2?
>
>> x<-c(2,0,0,2,2,2)
>> i<-1:length(x)
>> x[i]
> [1] 2 0 0 2 2 2
>> x[i+1]
> [1] ?0 ?0 ?2 ?2 ?2 NA
>
> can i be described using a loop that says go back to the beginning of list
> x?
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/creating-a-closed-loop-for-a-list-tp4221333p4221333.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.