# Add column and column names of variable lags to dataframe

**URL:** <https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [August 30, 2022, 8:57pm UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571 "2022-08-30T20:57:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Longblackcoffee](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@Longblackcoffee](https://discourse.julialang.org/u/Longblackcoffee)\
**Post date:** [August 30, 2022, 8:57pm UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571/1 "2022-08-30T20:57:47Z")

</div>

Hi everyone,

I have a dataset and I want to add a new column for every lag, with the column name being variable\_(lag number). So for example,

```julia
using DataFrames
using ShiftedArrays

test = DataFrame(a=1:10, b=11:20)

```

I want to add 5 columns with column a\_1 being just the original column A lagged once, column a\_2 being lagged twice, etc.

Doing this for just a few lags is fine with something like

```julia
test.a2 = lag(test.a,2)

```

However, I don’t know how to do this in a loop/scale this up, which I need since I need to create about 48 new variables.

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 30, 2022, 9:14pm UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571/2 "2022-08-30T21:14:04Z")

</div>

You could try this:

```julia
vars = "a" .* string.(1:48)
hcat(test, DataFrame( vars .=> lag.(Ref(test.a), 1:48)))

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 30, 2022, 9:14pm UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571/3 "2022-08-30T21:14:44Z")

</div>

See the following:

```julia
julia> using DataFrames, ShiftedArrays;

julia> df = DataFrame(a = 1:10, b = 11:20, c = 21:30);

julia> cols_to_add_lags = ["a", "b", "c"];

julia> for c in cols_to_add_lags
           for i in 1:5
               df[:, "$(c)_$(i)"] = lag(df[:, c], i)
           end
       end;

julia> df
10×18 DataFrame
 Row │ a b c a_1 a_2 a_3 a_4 a_5 ⋯
     │ Int64 Int64 Int64 Int64? Int64? Int64? Int64? Int64? ⋯
─────┼─────────────────────────────────────────────────────────────────────
   1 │ 1 11 21 missing missing missing missing missing ⋯
   2 │ 2 12 22 1 missing missing missing missing 
   3 │ 3 13 23 2 1 missing missing missing 
   4 │ 4 14 24 3 2 1 missing missing 
   5 │ 5 15 25 4 3 2 1 missing ⋯
   6 │ 6 16 26 5 4 3 2 1
   7 │ 7 17 27 6 5 4 3 2
   8 │ 8 18 28 7 6 5 4 3
   9 │ 9 19 29 8 7 6 5 4 ⋯
  10 │ 10 20 30 9 8 7 6 5
                                                         10 columns omitted

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 31, 2022, 8:08am UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571/4 "2022-08-31T08:08:02Z")

</div>

or

```julia
julia> transform!(test, [:a => (x -> lag(x, i)) => "a_$i" for i in 1:5])
10×7 DataFrame
 Row │ a b a_1 a_2 a_3 a_4 a_5
     │ Int64 Int64 Int64? Int64? Int64? Int64? Int64?
─────┼───────────────────────────────────────────────────────────
   1 │ 1 11 missing missing missing missing missing
   2 │ 2 12 1 missing missing missing missing
   3 │ 3 13 2 1 missing missing missing
   4 │ 4 14 3 2 1 missing missing
   5 │ 5 15 4 3 2 1 missing
   6 │ 6 16 5 4 3 2 1
   7 │ 7 17 6 5 4 3 2
   8 │ 8 18 7 6 5 4 3
   9 │ 9 19 8 7 6 5 4
  10 │ 10 20 9 8 7 6 5

```

---

<div class="post-metadata">

**Author:** ![Longblackcoffee](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@Longblackcoffee](https://discourse.julialang.org/u/Longblackcoffee)\
**Post date:** [August 31, 2022, 2:36pm UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571/6 "2022-08-31T14:36:37Z")

</div>

Worked like a charm, thanks so much everyone!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 1, 2022, 6:28am UTC](https://discourse.julialang.org/t/add-column-and-column-names-of-variable-lags-to-dataframe/86571/7 "2022-09-01T06:28:51Z")

</div>

```julia
julia> transform(df, :a=>(x->hcat(lag.([x], 1:5)...))=>string.("a_",1:5))
10×7 DataFrame
 Row │ a b a_1 a_2 a_3 a_4 a_5     
     │ Int64 Int64 Int64? Int64? Int64? Int64? Int64?
─────┼───────────────────────────────────────────────────────────
   1 │ 1 11 missing missing missing missing missing
   2 │ 2 12 1 missing missing missing missing
   3 │ 3 13 2 1 missing missing missing
   4 │ 4 14 3 2 1 missing missing
   5 │ 5 15 4 3 2 1 missing
   6 │ 6 16 5 4 3 2 1
   7 │ 7 17 6 5 4 3 2
   8 │ 8 18 7 6 5 4 3
   9 │ 9 19 8 7 6 5 4
  10 │ 10 20 9 8 7 6 5

```

```julia
julia> transform(df, :a=>(x->(;zip(Symbol.("a_",1:5),lag.([x], 1:5))...))=>AsTable)
10×7 DataFrame
 Row │ a b a_1 a_2 a_3 a_4 a_5     
     │ Int64 Int64 Int64? Int64? Int64? Int64? Int64?
─────┼───────────────────────────────────────────────────────────
   1 │ 1 11 missing missing missing missing missing
   2 │ 2 12 1 missing missing missing missing
   3 │ 3 13 2 1 missing missing missing
   4 │ 4 14 3 2 1 missing missing
   5 │ 5 15 4 3 2 1 missing
   6 │ 6 16 5 4 3 2 1
   7 │ 7 17 6 5 4 3 2
   8 │ 8 18 7 6 5 4 3
   9 │ 9 19 8 7 6 5 4
  10 │ 10 20 9 8 7 6 5

julia> transform(df, :a=>(x->NamedTuple(zip(Symbol.("a_",1:5),lag.([x], 1:5))))=>AsTable)
10×7 DataFrame
 Row │ a b a_1 a_2 a_3 a_4 a_5     
     │ Int64 Int64 Int64? Int64? Int64? Int64? Int64?
─────┼───────────────────────────────────────────────────────────
   1 │ 1 11 missing missing missing missing missing
   2 │ 2 12 1 missing missing missing missing
   3 │ 3 13 2 1 missing missing missing
   4 │ 4 14 3 2 1 missing missing
   5 │ 5 15 4 3 2 1 missing
   6 │ 6 16 5 4 3 2 1
   7 │ 7 17 6 5 4 3 2
   8 │ 8 18 7 6 5 4 3
   9 │ 9 19 8 7 6 5 4
  10 │ 10 20 9 8 7 6 5

```
