Circular shift of vector (equivalent to numpy.roll)
How about using head
and tail
...
roll <- function( x , n ){ if( n == 0 ) return( x ) c( tail(x,n) , head(x,-n) )}roll(1:5,2)#[1] 4 5 1 2 3# For the situation where you supply 0 [ this would be kinda silly! :) ]roll(1:5,0)#[1] 1 2 3 4 5
One cool thing about using head
and tail
... you get a reverse roll with negative n
, e.g.
roll(1:5,-2)[1] 3 4 5 1 2
Here's an alternative which has the advantage of working even when x
is "rolled" by more than one full cycle (i.e. when abs(n) > length(x)
):
roll <- function(x, n) { x[(seq_along(x) - (n+1)) %% length(x) + 1]}roll(1:5, 2)# [1] 4 5 1 2 3roll(1:5, 0)# [1] 1 2 3 4 5roll(1:5, 11)# [1] 5 1 2 3 4
FWIW (and not that it's worth much) it also works on data.frame
s:
head(mtcars, 1)# mpg cyl disp hp drat wt qsec vs am gear carb# Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4head(roll(mtcars, 2), 1)# gear carb mpg cyl disp hp drat wt qsec vs am# Mazda RX4 4 4 21 6 160 110 3.9 2.62 16.46 0 1