basic indexing
On Wed, 10 Oct 2001, Warnes, Gregory R wrote:
> -----Original Message----- > From: Duncan Murdoch [mailto:dmurdoch at pair.com] > > On Tue, 9 Oct 2001 21:00:32 -0400, Michaell Taylor
[michaell.taylor at reis.com]:
>
> >in stata this would be > >gen z=x[N-1]
> > In R use z <- c(NA, x[-length(x)])
This can be captured conveniently in the simple function: lag <- function(x, n) c( rep(NA,n), x[1:(length(x)-n)] ) which is used as
y
[1] 8 9 2 8 8
lag(y,1)
[1] NA 8 9 2 8
lag(y,2)
[1] NA NA 8 9 2
There is of course already a generic function called lag in R (in package ts), so overwriting it would not be a good idea. Especially since this one has argument n of the opposite sign to the generic definition.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._