Calculating the percentage change between two values ​​in R and turning off one problem

I am trying to calculate the percentage change between two points in R in the form:

(X_(i+1) - X_(i))/(X_(i)) 

Here is what I came up with so far:

 #x is a vector from the dataframe #lag is distance between two points being compared percent_change = function(x,lag = 1) { n = length(x) pchange = c((x[(1+lag):n] - x[1:(n-lag)])/x[1:(n-lag)],NA) return(pchange) } 

However, to complete this task in R, I had to bind NA to avoid:

 Error in \`$<-.data.frame\`(\`*tmp*\`, "Change", value = c(0.00248221082243916, : replacement has 4616 rows, data has 4617 

With this addition, the operation is performed and aligned with what I calculated, it should be on paper.

Is there a way that I should not add NA?

+4
source share
2 answers

You need NA if you want to save the pc_change result in the original data frame:

Since the last element of your array has no mapping to x+1 , it will generate a vector 1 (or lag) shorter than the original.


Warning: Please note that you added one NA - this is correct for the case lag=1 , but in general you will need lag Γ— NA elements.

Try replacing NA with rep(NA,lag) .


Here's a more compact version of your function using the built-in diff function:

 pcchange=function(x,lag=1) c(diff(x,lag),rep(NA,lag))/x 
+9
source

For me, adding NA seems like the right solution. However, there are functions to perform such operations. Take a look at the lag function to get lagging time series. In general, for time series analysis, look at the xts and zoo packages for processing time-series queries. CRAN TaskView, designed for time series , is also a valuable source of information.

+1
source

Source: https://habr.com/ru/post/1433252/


All Articles