# Create lead and lag variable in DataFrame

**URL:** <https://discourse.julialang.org/t/create-lead-and-lag-variable-in-dataframe/8658>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 28, 2018, 9:04pm UTC](https://discourse.julialang.org/t/create-lead-and-lag-variable-in-dataframe/8658 "2018-01-28T21:04:41Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [January 29, 2018, 11:37am UTC](https://discourse.julialang.org/t/create-lead-and-lag-variable-in-dataframe/8658/7 "2018-01-29T11:37:09Z")

</div>

Yep, you could do something like:

```julia
using ShiftedArrays
v = lead(price) .- 2 .* price .+ lag(price)

```

which avoids unnecessary allocations. I think it works best on Julia 0.7 due to recent improvements on missing data handling by @nalimilan, but if you don’t think you’ll encounter performance issues it should be usable already.

To integrate this functionality in DataFrames, I guess the easiest is to add ShiftedArrays as a DataFrames dependency (ShiftedArrays is a small pure Julia package) and reexport `lead` and `lag`.

---

_[View the full topic](https://discourse.julialang.org/t/create-lead-and-lag-variable-in-dataframe/8658)._
