[R] adress the current index

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Tue May 25 17:25:37 CEST 2004


Mag. Ferri Leberl wrote:

> How can I adress the current index of a vector?
> 
> I want to work with time series and therefore give the n-th element of a 
> vector some value dependent on the value of the n-1th element.

Sorry, your question is not really clear, but possibly the following may 
help:

1) If you have a vector x, you can create a shifted vector by removing 
the first element, e.g.:

xnew <- x[-1] + somevalue # or apply a function f(x[-1])

2) The last element can be removed with:

xnew <- x[-length(x)]

3) ... and sometimes vector concatenation ?c or the ?ifelse function can 
be useful in such circumstances.

Thomas P.

PS: there is more about such things in the docs.




More information about the R-help mailing list