Quickly create many lagged time series features with R's data.table

When adapting a non-time-series model to time-series data, it's often useful to be able to quickly create lagged variables. For example, lets say you have an R data.table with:

  • ticker_id
  • timestamp
  • open
  • high
  • low
  • close
Given this data is in a data.table we can call the following function to create several lagged variables.
Now our data will contain the original columns plus lagged columns with the same column names with appended lag identifier.

Have fun 'Data Sciencing'!

Popular Posts