Last Updated: February 25, 2016
· christophergandrud

R lagged data with time-series cross-sectional data.

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: Country, Year and Variable. We want to lag Variable one year for each country. Let's call the lagged variable VariableLag1. Use the ddply command like this:


Data <- ddply(Data, .(country), transform, VariableLag1 =
                        c(NA, Variable[-length(Variable)]

That's it.

Thanks to this post on StackOverflow.