[R] basic indexing

Duncan Murdoch dmurdoch at pair.com
Wed Oct 10 04:22:26 CEST 2001


On Tue, 9 Oct 2001 21:00:32 -0400, you wrote:

>in stata this would be
>gen z=x[N-1]

In R use z <- c(NA, x[-length(x)])

This says that z is an NA followed by the x vector with the last
element left off.  Another way is

 z <- c(NA, x)
 length(z) <- length(x)

>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

A one-liner is

  z <- ifelse(y == 8, c(NA, x[-length(x)]), NA)

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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