'Compute 2-year and 3-year changes of a multivariate quarterly Time Series
I'm working with a multivariate quarterly Time Series and for each variable, I would like to compute 2-year and 3-year changes and store them in a separate column, so far I'm running the following code for the diff operation by I don't know how to write a for loop to do it for all the variables and store them on a separate column(should also keep that in mind that after each diff data become smaller)
x_2y=x[dif_2y:length(x)]-x[1:(length(x)-dif_2y+1)]
x_3y=x[dif_3y:length(x)]-x[1:(length(x)-dif_3y+1)]
The sample of my dataset is as follows:
structure(list(Fraud = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ValueDate = structure(c(1456790400,
1464739200, 1472688000, 1480550400, 1488326400, 1496275200, 1504224000,
1512086400, 1519862400, 1527811200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Total_Value = c(71.833, 45.6, 37.06, 38.8,
22.89, 37.99, 30.45, 41.2, 61.1, 70.21), Stock_Value = c(22.78,
33.76, 40.21, 50.32, 66.2, 61.9, 56.21, 43.9, 34.9, 53.9)), row.names = c(NA,
10L), class = "data.frame")
so I would like to have the 2-year and 3-year changes of Total_Value and Stock_Value in two different columns.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
