FW: [R] basic indexing
Warnes, Gregory R
gregory_r_warnes at groton.pfizer.com
Thu Oct 11 01:31:39 CEST 2001
> -----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
> >in stata this would be
> >gen z=x[N-1] if y==8
>
> In R you should probably use two statements for this. The first
> constructs z as above, the second is
>
> z[y != 8] <- NA
and, of course,
> z <- ifelse(y==8, lag(y,1), NA )
-Greg
LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list