I know there is a simple answer, I am attempting to make a simple indexing issue too complex. Suppose I have a dataframe like the following: x y 1 5 8 2 8 9 3 1 2 4 6 8 5 9 8 Now suppose I simply wish to have a lagged indicator of x such that x y z 1 5 8 NA 2 8 9 5 3 1 2 8 4 6 8 1 5 9 8 6 in stata this would be gen z=x[N-1] or even x y z 1 5 8 NA 2 8 9 NA 3 1 2 NA 4 6 8 1 5 9 8 6 in stata this would be gen z=x[N-1] if y==8 I've tried : z _ x[-1], but this leaves the vector too short. I could "backfill" but this seem pretty contrived. In practice, I need to get flexible lagging system as appropriate lags may depend of variable combinations. Thanks for any comments you may have. Michaell -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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==8In 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 9 Oct 2001, Michaell Taylor wrote:> Suppose I have a dataframe like the following: > > x y > 1 5 8 > 2 8 9 > 3 1 2 > 4 6 8 > 5 9 8 > > Now suppose I simply wish to have a lagged indicator of x such that > > x y z > 1 5 8 NA > 2 8 9 5 > 3 1 2 8 > 4 6 8 1 > 5 9 8 6 > > in stata this would be > gen z=x[N-1]In R z <- c(NA,z[1:(length(z)-1)] Agus -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._