[R] Help with conditional lagging of data
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jun 5 03:52:39 CEST 2007
Seems you want to diff X, not lag it.
We can either maintain the long form of the data and do it as in #1
or convert the data to "wide" form and do it as in #2 which is most
convenient using zoo where we make the individual time series into
zoo series, merge them and then apply diff:
Lines <- "ID Year X
AB12 2000 100
AB12 2001 120
AB12 2002 140
AB12 2003 80
BL14 2000 180
BL14 2001 150
CR93 2000 45
CR93 2001 49
CR93 2002 56
CR93 2003 67
"
DF <- read.table(textConnection(Lines), header = TRUE)
# 1
f <- function(DF) cbind(DF[,1:2], diff = c(NA, diff(DF$X)))
DF.by <- by(DF, DF$ID, f)
do.call("rbind", DF.by)
# 2
library(zoo)
fz <- function(DF) zoo(DF$X, DF$Year)
diff(do.call("merge", by(DF, DF$ID, fz)), na.pad = TRUE)
For more info on zoo:
library(zoo)
vignette("zoo")
On 6/4/07, Anup Nandialath <anup_nandialath at yahoo.com> wrote:
> Dear Friends,
>
> I have some data with three columns named ID, Year and Measure X. I need to create a column which gives me a lag for each ID (note not a continous lag), but a lag conditional on the id and the given year. Please find below a sample of the data
>
> Input file sample
>
> ID Year X
>
> AB12 2000 100
> AB12 2001 120
> AB12 2002 140
> AB12 2003 80
> BL14 2000 180
> BL14 2001 150
> CR93 2000 45
> CR93 2001 49
> CR93 2002 56
> CR93 2003 67
>
> Expected output from this data
>
> ID Year Xlag
> AB12 2000 .
> AB12 2001 20
> AB12 2002 20
> AB12 2003 -60
> BL12 2000 .
> BL14 2001 -30
> CR93 2000 .
> CR93 2001 5
> CR93 2002 7
> CR93 2003 9
>
> Can somebody please help me with how to implement this in R. Thanks.
>
> Sincerely
>
> Anup
>
>
>
> ---------------------------------
> Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list