I have a data set DF with the following data
| Zone | Year | X | Y |
|---|---|---|---|
| 1001 | 2018 | 10 | 5 |
| 1001 | 2019 | 20 | 10 |
| 1001 | 2020 | 30 | 20 |
| 1002 | 2018 | 15 | 10 |
| 1002 | 2019 | 25 | 20 |
| 1002 | 2020 | 35 | 40 |
I want to create a column Z = X + Y - Previous year's Y So it creates the following Table:
| Zone | Year | X | Y | Z |
|---|---|---|---|---|
| 1001 | 2018 | 10 | 5 | NA |
| 1001 | 2019 | 20 | 10 | 25 |
| 1001 | 2020 | 30 | 20 | 40 |
| 1002 | 2018 | 15 | 10 | NA |
| 1002 | 2019 | 25 | 20 | 35 |
| 1002 | 2020 | 35 | 40 | 55 |
I can use "mutate" from DPLYR to generate column Z: mutate(DF, Z = X + Y - lag(Y))
I can use tapply to apply recursively on DF. Can I create a function using DPLYR in a user-defined function to apply this using tapply later?