### R Lagged Variables 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. Just remember to have the variables in time order.

Thanks to this post on StackOverflow.