Search code examples
group-byjulialag

What is the equivalent in Julia for R code - slide with group by


What is the equivalent of below R code in Julia

df<-slide(df,Var="m",GroupVar="symbol",NewVar="m_1",slideBy = -1,reminder = FALSE)

In R this code creates a new lagged variable "m_1" for "m" grouped by "symbol" variable and sliding or lagging by 1.


Solution

  • There isn't to my knowledge any Julia package that implements this functionality out of the box, but you can get there with DataFrames and ShiftedArrays:

    julia> using DataFrames, ShiftedArrays
    
    julia>  df = DataFrame(symbol = ["a","a","a","b","b","b"], m = [1,2,3,4,5,6])
    6×2 DataFrame
    │ Row │ symbol │ m     │
    │     │ String │ Int64 │
    ├─────┼────────┼───────┤
    │ 1   │ a      │ 1     │
    │ 2   │ a      │ 2     │
    │ 3   │ a      │ 3     │
    │ 4   │ b      │ 4     │
    │ 5   │ b      │ 5     │
    │ 6   │ b      │ 6     │
    
    julia> by(df, :symbol, m_lag = :m => lag)
    6×2 DataFrame
    │ Row │ symbol │ m_lag   │
    │     │ String │ Int64⍰  │
    ├─────┼────────┼─────────┤
    │ 1   │ a      │ missing │
    │ 2   │ a      │ 1       │
    │ 3   │ a      │ 2       │
    │ 4   │ b      │ missing │
    │ 5   │ b      │ 4       │
    │ 6   │ b      │ 5       │