I have a time series dataframe in R, lets call it df, that looks like this:
| Date | City_1 | City_2 | City_3 | ..... | City_n |
|---|---|---|---|---|---|
| 1980-01-01 | 1 | 4 | 6 | ... | 2 |
| 1980-01-02 | 2 | 7 | 6 | ... | 5 |
| 1980-01-03 | 3 | 7 | 1 | ... | 1 |
| 1980-01-04 | 8 | 1 | 8 | ... | 1 |
| 1980-01-05 | 4 | 3 | 5 | ... | 0 |
| ... | ... | ... | ... | ... | 7 |
| 2020-08-20 | 3 | 1 | 8 | ... | 2 |
| 1980-08-21 | 7 | 8 | 3 | ... | 2 |
| 1980-08-22 | 8 | 6 | 5 | ... | 0 |
| 1980-08-23 | 9 | 8 | 2 | ... | 1 |
Here is a reproducible part of my data:
df <- structure(c(17.86, 18.65, 18.42, 19.21, 21.34, 11.35, 10.01,
9.67, 10.12, 10.81, 20.77, 20.99, 20.88, 20.91, 20.5, 13.8, 13.63,
13.6, 13.02, 13.87, 22.07, 22.78, 23.24, 23.33, 24.06), .Dim = c(5L,
5L), class = c("xts", "zoo"), index = structure(c(315532800,
315619200, 315705600, 315792000, 315878400), tzone = "UTC", tclass = "Date"),
.Dimnames = list(NULL, c("Sao Paulo", "Mexico City", "Lima", "Bogota",
"Rio de Janeiro")))
And I want to create a new dataframe that, in each city column, for each date, has the percentage change with respect to the same date the previous year, given by the following formula:
change = [x(t) -x(t-1)]/x(t)
To get a final dataframe, df2 that will look like this:
| Date | City_1 | City_2 | City_3 | ..... | City_n |
|---|---|---|---|---|---|
| 1981-01-01 | a | j | s | ... | b |
| 1981-01-02 | b | k | t | ... | c |
| 1981-01-03 | c | l | u | ... | d |
| 1981-01-04 | d | m | v | ... | t |
| 1981-01-05 | e | n | w | ... | e |
| ... | ... | ... | ... | ... | f |
| 2020-08-20 | f | o | x | ... | h |
| 1980-08-21 | g | p | y | ... | s |
| 1980-08-22 | h | q | z | ... | y |
| 1980-08-23 | i | r | a | ... | u |
Where all the letters are the percentage change of each day, respect last year.
I'm really new at R, but I think that I would need a function. Can you guys help me out?