[R] basic indexing

Agustin Lobo alobo at ija.csic.es
Wed Oct 10 08:48:36 CEST 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list