Havent been able to find anything similar, and new to Julia.
Trying to see if this can be done in a single process, or should be split up, or something else I am not thinking of. Basically as the df below shows - trying to see how i can add a conditional logic column to this df, anchored on Year column (apologies for the int64 - real data is actually a Date df format).
Specifically what would be best way to add a trailing 2 year column for the sample (next to the column that shows overall growth ongoing - in the example df, its the ProValue column), something akin to:
"ProValue2YrTrailing = cumprod(:Growth .+1) when Count of years is 2 per group specified"
Cant quite figure out how to use @linq and Dataframes here to create a conditional column via transform.
using DataFramesMeta
df = DataFrame(region = ["US","US","US","US","US","EU","EU","EU","EU","EU"],
product =
["apple","apple","apple","banana","banana","apple","apple","banana","banana","banana"],
year = [2009,2010,2011,2010,2011,2010,2011,2009,2010,2011],
Growth = [0.13,0.23,0.05,0.22,0.28,0.24,0.23,0.03,0.17,0.18])
df = @linq df |>
groupby([:region,:product]) |>
transform(ProValue = cumprod(:Growth .+1))
thanks!
edit: One way i can think of doing is by below, but doesnt seem very elegant, especially as periods frames grow from 2 to say 30:
df = @linq df |>
groupby([:region,:product]) |>
transform(ProValueTrailing2 = ["missing"; rolling(prod, :Growth .+1, 2)])
Found last night RollingFunctions.jl package, which solves the problem as well. It has a Running function, which you can use same way as Rolling but without the issues of missing items. So something like this: `transform(Growth = running(prod, :Provalue .+1, 2))` – SubT Sep 03 '21 at 12:39