Calculating YoY Rate of Change on DF

Hello Folks:

I have a DF that looks like:

Year = (2016:1:2021,6)
Col1= rand(60:0.5:65, 6)
Col2 = rand(20:0.5:25, 6)

I would like to calculate the rate of
change starting from year 2016 for
both Col1 and Col2.

Might you have some methods to
share?

Thanks,

I think you want
[missing; diff(df.Col1)] ?

Or with a different definition of rate of change:

df[!,:ROC1] = [missing; (df.Col1[2:end]./df.Col1[1:end-1] .- 1)*100]
2 Likes

@rafael.guerra Thank you for this!

I applied the following modification for presentation purposes:

df[!,:ROC1] = [missing; (round.(df.Col1[2:end] ./ df.Col1[1:end-1] .- 1, digits=2)) .*100]
1 Like

I would do

using ShiftedArrays
@transform! df :Col1_diff = :Col1 .- lag(:Col1)
2 Likes