PeriodicVector(1:4, period = 4)
PeriodicVector(1:4) ## same
new("PeriodicVector", 1:4, period = 4)
## if period is given but x is missing, the vector is filled with NA's
PeriodicVector(period = 4)
## this throws error, since length(x) != period:
## PeriodicVector(1:3, period = 4)
## extract
x <- PeriodicVector(1:4)
x[3:12]
x[c(3, 7, 11, 15)]
# any indices in (-Inf, Inf) work
x[0]
x[-3:0]
## "[<-" works on the underling vector
x[1] <- 11; x
## modulo indexing works also in assignments:
x[5] <- 21; x
## empty index returns the underlying vector
x[]
## the recycling rule applies on assignment
x[] <- 9; x
x[] <- 1:2; x
## this gives warning, as for numeric vectors
## x[] <- 8:1
## compare:
## x <- 1:4
## x[] <- 8:1
## arithmetic works as usual:
2 * x
x + 1:4
## x + 1:3 # warning - '... a multiple ...'
Run the code above in your browser using DataLab