GaryBAYLOR / R-code

A collection of algorithms written by myself for solving statistical problems
0 stars 0 forks source link

A funny function to redefine "[" #21

Open GaryBAYLOR opened 9 years ago

GaryBAYLOR commented 9 years ago

This function redefine the use of "[". When the index goes beyond the length of the vector, it just restart from beginning.

`[` <- function(x, k) {
    n <- length(x)
    k <- k %% n
    if(k == 0) k <- n
    subset(x, 1:n == k)

}
> x
[1] 3 2 4 5 1
> x[1]
[1] 3
> x[2]
[1] 2
> x[3]
[1] 4
> x[4]
[1] 5
> x[5]
[1] 1
> x[6]
[1] 3
> x[7]
[1] 2