If you want to create a lagged variable in R for time-series cross-sectional data the usual time series packages (i.e. zoo and xts) don't really do the job.
So use the plyr package.
Imagine we have a data frame (
Data) with three variables:
Variable. We want to lag
Variable one year for each country. Let's call the lagged variable
VariableLag1. Use the
ddply command like this:
library(plyr) Data <- ddply(Data, .(country), transform, VariableLag1 = c(NA, Variable[-length(Variable)] ) )
Thanks to this post on StackOverflow.