[R] basic indexing

Michaell Taylor michaell.taylor at reis.com
Wed Oct 10 03:00:32 CEST 2001


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



More information about the R-help mailing list