Re: [R] creating a closed loop for a list

2011-12-21 Thread jim holtman
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 +

[R] creating a closed loop for a list

2011-12-21 Thread djrwicks
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 loo