Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:22:14+00:00 2026-06-11T01:22:14+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:22:16+00:00Added an answer on June 11, 2026 at 1:22 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to calculate a distance between two sets of coordinate points in an
Essentially I'm trying to calculate the bisector line between two points. I've got two
I'm trying to calculate elapsed hours between two times. When calculating PM to AM
I'm trying to calculate the difference between two dates in weeks of year. I
I am trying to calculate the time between two times on the current date
I'm trying to calculate the number of days between two days, but I'm running
I am trying to calculate squared Euclidean distance between two 4d float vectors using
I am trying to calculate the hour difference between two times. What I've been
Friends,I am trying to calculate the difference between two date&time entries that the user
Im trying to calculate the how many days there are between two dates and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.