# Not able to use lag and lead function

**URL:** <https://discourse.julialang.org/t/not-able-to-use-lag-and-lead-function/91142>\
**Category:** New to Julia\
**Tags:** package, dataframes\
**Created:** [December 2, 2022, 11:30am UTC](https://discourse.julialang.org/t/not-able-to-use-lag-and-lead-function/91142 "2022-12-02T11:30:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [December 2, 2022, 11:30am UTC](https://discourse.julialang.org/t/not-able-to-use-lag-and-lead-function/91142/1 "2022-12-02T11:30:42Z")

</div>

Hi i just tried to sun a simple exmaple which is given here  
[https://stackoverflow.com/questions/61038354/create-lag-lead-time-series-with-by-groups-in-julia](https://stackoverflow.com/questions/61038354/create-lag-lead-time-series-with-by-groups-in-julia)

```julia
using DataFrames, ShiftedArrays

df1 = DataFrame(var1=["a","a","a","a","b","b","b","b"],
                                    var2=[0,1,2,3,0,1,2,3]);
by(df1, :var1, var2_l2 = :var2 => Base.Fix2(lag, 2))

```

but it resulted in below error

```julia
julia> by(df1, :var1, var2_l2 = :var2 => Base.Fix2(lag, 2))
ERROR: UndefVarError: lag not defined
Stacktrace:
 [1] top-level scope
   @ ~/data/code/NICU_DEC_2022/data_wrangling.jl:105

```

Is there any other way to get lag and lead values of a column.  
I used to use below code earler, but its not working now

```julia
transform!(groupby(df,:ID),:TIME => lag, :TIME => lead)

```

Thanks in advance

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 2, 2022, 5:10pm UTC](https://discourse.julialang.org/t/not-able-to-use-lag-and-lead-function/91142/2 "2022-12-02T17:10:30Z")

</div>

The function names just aren’t imported in the latest `ShiftedArrays` version, so you need to either qualify `ShiftedArrays.lead` or import them explicitly:

```julia
julia> using DataFrames; import ShiftedArrays: lead, lag

julia> transform(DataFrame(TIME = rand(5)), :TIME .=> [lag, lead])
5×3 DataFrame
 Row │ TIME TIME_lag TIME_lead      
     │ Float64 Float64? Float64?       
─────┼──────────────────────────────────────────
   1 │ 0.543515 missing 0.745711
   2 │ 0.745711 0.543515 0.181031
   3 │ 0.181031 0.745711 0.765985
   4 │ 0.765985 0.181031 0.201351
   5 │ 0.201351 0.765985 missing        

```
