[R] Lag variable by group

Janka VANSCHOENWINKEL janka.vanschoenwinkel at uhasselt.be
Mon Sep 7 13:18:25 CEST 2015


Hi!

I have the following dataset with the variables ID (this is a unique ID per
farmer), year, and another variable t1.
I now would like to have a fourth variable which is the lag value of t1 for
each farm ID.

I found a code on the internet that does exactly what I need, but it does
not work for this dataset. Does anyone have suggestions about how I can
make this work?

Thanks a lot!

Janka

data<-structure(list(year = c(2007, 2005, 2008, 2006, 2005, 2007, 2006,
2008, 2007, 2005, 2007, 2007, 2005, 2006, 2005, 2006, 2005, 2006,
2007, 2007, 2005, 2008, 2007, 2008, 2005, 2005, 2006, 2008, 2007,
2007, 2008, 2008, 2006, 2005, 2007, 2006, 2008, 2008, 2007, 2007,
2007, 2006, 2006, 2008, 2006, 2008, 2008, 2008, 2006, 2007, 2008,
2007, 2005, 2007, 2008, 2005, 2007, 2005, 2005, 2008, 2005, 2006,
2005, 2006, 2008, 2006, 2008, 2006, 2007, 2006, 2005, 2008, 2006,
2007, 2008, 2006, 2006, 2006, 2005, 2008, 2006, 2008, 2006, 2006,
2006, 2007, 2008, 2005, 2007, 2006, 2007, 2008, 2006, 2008, 2005,
2007, 2005, 2007, 2006, 2006), id = c(28958L, 28962L,
28962L, 28965L, 28960L, 28962L, 28964L, 28970L, 28961L, 28965L,
78458L, 28960L, 28961L, 28961L, 28969L, 28962L, 28959L, 28959L,
58845L, 28965L, 28963L, 78459L, 28967L, 28957L, 28964L, 28966L,
28958L, 28960L, 28969L, 28959L, 28958L, 28969L, 58845L, 28958L,
28954L, 28963L, 78458L, 28965L, 28966L, 28963L, 28970L, 28970L,
28960L, 28959L, 28954L, 28954L, 58845L, 28967L, 28966L, 78459L,
28956L, 28964L, 28956L, 28957L, 28961L, 28970L, 28968L, 28954L,
28955L, 28968L, 28968L, 28967L, 28967L, 28957L, 28966L, 28956L,
28964L, 28969L, 28955L, 28955L, 28957L, 28955L, 28968L, 28956L,
28963L, 29004L, 58848L, 29005L, 28974L, 29005L, 28974L, 29006L,
28981L, 29007L, 29002L, 28980L, 29001L, 29006L, 29005L, 28989L,
28989L, 58846L, 28980L, 28981L, 78467L, 28990L, 28973L, 29004L,
28972L, 29006L), t1 = c(-1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.81807494163513, -1.81807494163513, -1.81807494163513,
-1.81807494163513, -1.43884992599487, -1.43884992599487, -1.43884992599487,
-1.43884992599487, -1.43884992599487, -1.43884992599487, -1.43884992599487,
-1.43884992599487, -1.43884992599487, -1.43884992599487, -1.43884992599487,
-1.43884992599487, -1.43884992599487, -1.43884992599487, -1.43884992599487,
-1.43884992599487, -1.43884992599487, -1.43884992599487, -1.43884992599487,
-1.43884992599487, -1.43884992599487, -1.43884992599487, -1.43884992599487,
-1.43884992599487, -1.43884992599487)), .Names = c("year", "id",
"t1"), row.names = c(NA, 100L), class = "data.frame")

library(data.table)
data[, lag.t1:=c(NA, t1[-.N]), by=id]


Thank you very much!

Janka

	[[alternative HTML version deleted]]



More information about the R-help mailing list