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 7885845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:10:27+00:00 2026-06-03T05:10:27+00:00

I know it’s not the best practice in R to use the for loop

  • 0

I know it’s not the best practice in R to use the for loop because it doesn’t have an enhanced performance. For almost all cases there is a function of the family *apply that solves our problems.

However I’m facing a situation where I don’t see a workaround.

I need to calculate percent variation for consecutive values:

pv[1] <- 0
for(i in 2:length(x)) {
  pv[i] <- (x[i] - x[i-1])/x[i-1]
}

So, as you can see, I have to use both the x[i] element, but also the x[i-1] element. By using the *apply functions, I just see how to use the x[i]. Is there anyway I can avoid the forloops?

  • 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-03T05:10:29+00:00Added an answer on June 3, 2026 at 5:10 am

    What you offered would be the fractional variation, but if you multiplied by 100 you get the “percent variation”:

    pv<- vector("numeric",length(x))
    pv[1] <- 0
    pv[-1] <- 100* ( x[-1] - x[-length(x)] )/ x[-length(x)]
    

    Vectorized solution. ( And you should note that for-loops are going to be just as slow as *apply solutions … just not as pretty. Always look for a vectorized approach.)

    To explain a bit more: The x[-length(x)] is the vector, x[1:(length{x-1)], and the x[-1] is the vector, x[2:length(x)], and the vector operations in R are doing the same operations as in your for-loop body, although not using an explicit loop. R first constructs the differences in those shifted vectors, x[-length(x)] - x[-1], and then divides by x[1:(length{x-1)].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are many topics with similar issues, but I have not been
I know it's not a good idea to loop SQL queries, so I would
I know you have to synchronize your threads to be able to change your
I know that lot of questions about HTML sanitizers have appeared in SO, but
I know that all of these will be compiled together into one file but
I know how to use the search feature on JQGrid where you send the
I know that in Rails all ActiveRecord callbacks are executed inside transaction. So if
I know of cufon and one other similar. But what is the best way
I know the title isn't informative at all, but I had no idea how
I know the input have the maxlength, but I would like to have minlength

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.