Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the percent change between two values in R and off by one issue

Tags:

function

r

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

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

Here is what I have come 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, in order to accomplish this task in R I had to bind an 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 occurs and aligns to what I've calculate it should be on paper.

Is there a way where I do not have to append an NA?

like image 485
coatless Avatar asked Dec 20 '22 16:12

coatless


1 Answers

You do need the NA if you want to store the pc_change result back in the original data frame:

Since the last element of your array does not have an x+1 to compare to it will produce a vector 1 (or lag) shorter than the original.


Warning: Note that you have one NA added - this is correct for the case lag=1 but more generally you need 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
like image 181
Alex Brown Avatar answered Jan 30 '23 20:01

Alex Brown