[R] lagged variable in a dataframe

Cecilia Carmo cecilia.carmo at ua.pt
Fri Apr 24 12:11:15 CEST 2009


Hi!
I have the following dataframe which represents a 
financial variable:
> revenue
          firm year revenue id
10.1  500194670 2007  27725 10
10.2  500194670 2006   9717 10
10.3  500194670 2005 125621 10
10.4  500194670 2004  84837 10
12.1  500278725 2007 308532 12
12.2  500278725 2006 493755 12
12.3  500278725 2005 238195 12
12.4  500278725 2004  35472 12
13.1  502616695 2007    725 13
13.2  502616695 2006  14392 13
13.3  502616695 2005  32502 13
13.4  502616695 2004  35271 13


And I want to compute the lagged version of it, like this:
           firm year revenue id
10.1  500194670 2007  9717 10
10.2  500194670 2006 125621 10
10.3  500194670 2005 84837 10
10.4  500194670 2004  NA 10
12.1  500278725 2007 493755 12
12.2  500278725 2006 238195 12
12.3  500278725 2005 35472 12
12.4  500278725 2004  NA 12
13.1  502616695 2007  14392 13
13.2  502616695 2006  32502 13
13.3  502616695 2005  35271 13
13.4  502616695 2004  NA 13

I’ve tried lag(revenue,k=1) but I failed. The error 
message was:
Error in attr(x, "tsp") <- c(1, NROW(x), 1) :
   invalid time series parameters specified

Thank you for your help,
Cecília Carmo (Portugal)




More information about the R-help mailing list