Skip to content

basic indexing

3 messages · Warnes, Gregory R, Brian Ripley, Peter Dalgaard

#
>  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
[1] 8 9 2 8 8
[1] NA 8 9 2 8
[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,
-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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Wed, 10 Oct 2001, Warnes, Gregory R wrote:

            
There is of course already a generic function called lag in R (in package
ts), so overwriting it would not be a good idea.

Especially since this one has argument n of the opposite sign to the
generic definition.
#
Prof Brian D Ripley <ripley at stats.ox.ac.uk> writes:
...which is Splus compatible, although not desirable. From the Splus
docs:

NOTE:
    
   Logically k should have the opposite sign, but due to its pervasive
   use in other functions, lag remains unchanged. However, the new shift
   function (which calls lag by default) has the more logical
   convention.