I have data of multiple years (in this example only 3). Need to calculate year to date and 12 months variations in r. Year-to-date variations are as follows:
- Year to date variation January 2021= Value January 2021/ Value January 2020.
- Year to date variation February 2021= Sum(Value January 2021: February 2021)/Sum(Value January 2020: February 2020)
- Year to date variation March 2021= Sum(Value January 2021: March 2021)/Sum(Value January 2020: March 2020)
12 months variations are as follows:
12 months variation January 2021= Sum (Value February 2020:Value January 2021)/ Sum(Value February 2019:Value January 2020).
12 months variation February 2021= Sum (Value March 2020:Value February 2021)/ Sum(Value March 2019:Value February 2020).
Year Month Value Year to date variations R 12 mothns 2019 1 182 2019 2 160 2019 3 170 2019 4 123 2019 5 165 2019 6 153 2019 7 152 2019 8 182 2019 9 156 2019 10 141 2019 11 161 2019 12 193 2020 1 143 0,785714285714286 2020 2 138 0,821637426900585 2020 3 113 0,76953125 2020 4 127 0,820472440944882 2020 5 107 0,785 2020 6 185 0,853095487932844 2020 7 125 0,848868778280543 2020 8 109 0,813519813519814 2020 9 167 0,841302841302841 2020 10 128 0,847222222222222 2020 11 139 0,848710601719198 2020 12 173 0,853457172342621 2021 1 111 0,776223776223776 0,854133754607688 2021 2 169 0,99644128113879 0,88066062866276 2021 3 125 1,02791878172589 0,914835164835165 2021 4 134 1,03454894433781 0,916666666666667 2021 5 112 1,03662420382166 0,94960362400906 2021 6 114 0,940959409594096 0,89321468298109 2021 7 171 0,997867803837953 0,932806324110672 2021 8 161 1,04775549188157 1,00353356890459 2021 9 195 1,06425041186161 1,01345816266823 2021 10 150 1,07451564828614 1,03419811320755 2021 11 181 1,09588116137745 1,07287933094385 2021 12 163 1,07980652962515 1,07980652962515