8lwnew
Last Updated: February 25, 2016
·
2.912K
· christophergandrud
97142016bfb86beaecef6e3251c82a98

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:

library(plyr)

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

That's it.

Thanks to this post on StackOverflow.

Say Thanks
Respond