Gaurav Bansal
Gaurav Bansal

Reputation: 5660

Reorder a vector with wrap around in R

Let's say I have a simple vector x in R. It is in the order 'a','b','c','d'. Is there a function that would take the vector and reorder it with wrap around? For example, how can I get x to be 'c','d','a','b'?

#Vector x
> x <- letters[1:4]
> x
[1] "a" "b" "c" "d"

#What I want:
> somefcn(x, 3)
[1] "c" "d" "a" "b"

Upvotes: 4

Views: 473

Answers (1)

denis
denis

Reputation: 5673

x <- letters[1:4]
shiftnum <- 3
c(x[shiftnum:length(x)],x[1:shiftnum-1])

[1] "c" "d" "a" "b"

Is a very rough way to do, but it works

Upvotes: 3

Related Questions