I wrote: I found myself wishing for a function to rotate a vector. Is there one? I know about ?lag, but help.search("rotate") didn't find anything to the point.
Here I was regarding a vector as a _sequence_. The (one-step) rotation of c(u,v,w,x,y,z) is c(v,w,x,y,z,u). This is pretty much the way APL uses the word "rotate" (the vertical-bar-overstruck-with-a-circle operator).
Do you want:
c(x[-1],x[1]) for a one-step 'rotation'?
You could write a function:
function(x,n){ c(x[-(1:n)],x[1:n]) }
> rotVec(1:10,5) [1] 6 7 8 9 10 1 2 3 4 5
except this fails for n=0 or n>length(x). Ah.
function(x,n){ n <- 1+(n-1)%%length(x) c(x[-(1:n)],x[1:n])} }
seems to work. Even for negative n:
> rotVec(1:10,-1) [1] 10 1 2 3 4 5 6 7 8 9
Baz
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help