Skip to content
Prev 68650 / 398506 Next

Time series indexes

Thanks, Gabor. Reading your suggestion (and a previous one as well) I
realized I surely expressed myself quite badly when asking the
question.

Luckily one person privately suggested the following solution, which
is exactly what I was looking for:

x[time(x)==2] <- 0

This works wonderfully. However, I tested it and it is very slow. So I
am back to index arithmetic, which is fast:

x[2 - start(x)[1] + 1] <- 0 

However, this is much more prone to errors than the first solution above. 

I also tried to define get() and set() functions for time series as follows:

ts.get <- function(myts, i) { myts[i - start(myts)[1] + 1] }
ts.set <- function(myts, i, val) { myts[i - start(myts)[1] + 1] <- val; myts } 

Both of these work, and ts.get is quite useful. However, ts.set is
again slow due to the need to copy the whole object. So ts.set cannot
be used just as

ts.set(x, i, val)

but rather one must write

x <- ts.set(x, i, val)

In the definition of ts.set, the expression after the semicolon
returns a modified copy of the argument myts. It has to be done this
way (please let me know if there is a better and fast way) since in R
there is no way of passing arguments by reference. That is, one cannot
have side effects on the arguments of a function.

Cheers,

FS
On 4/27/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: