Skip to content
Advertisement

Circular shift of vector (equivalent to numpy.roll)

I have a vector:

JavaScript

And I’d like to do something like:

JavaScript

Is there a function like that in R? I’ve been googling around, but “R Roll” mostly gives me pages about Spanish pronunciation.

Advertisement

Answer

How about using head and tail

JavaScript

One cool thing about using head and tail… you get a reverse roll with negative n, e.g.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement