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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:57:35+00:00 2026-05-26T10:57:35+00:00

I have a data frame with around 25000 records and 10 columns. I am

  • 0

I have a data frame with around 25000 records and 10 columns. I am using code to determine the change to the previous value in the same column (NewVal) based on another column (y) with a percent change already in it.

x=c(1:25000)
y=rpois(25000,2)
z=data.frame(x,y)
z[1,'NewVal']=z[1,'x']

So I ran this:

for(i in 2:nrow(z)){z$NewVal[i]=z$NewVal[i-1]+(z$NewVal[i-1]*(z$y[i]/100))}

This takes considerably longer than I expected it to. Granted I may be an impatient person – as a scathing letter drafted to me once said – but I am trying to escape the world of Excel (after I read http://www.burns-stat.com/pages/Tutor/spreadsheet_addiction.html, which is causing me more problems as I have begun to mistrust data – that letter also mentioned my trust issues).

I would like to do this without using any of the functions from packages as I would like to know what the formula for creating the values is – or if you will, I am a demanding control freak according to that friendly missive.

I would also like to know how to get a moving average just like rollmean in caTools. Either that or how do I figure out what their formula is? I tried entering rollmean and I think it refers to another function (I am new to R). This should probably be another question – but as that letter said, I don’t ever make the right decisions in my life.

  • 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-05-26T10:57:36+00:00Added an answer on May 26, 2026 at 10:57 am

    The secret in R is to vectorise. In your example you can use cumprod to do the heavy lifting:

    z$NewVal2 <- x[1] * cumprod(with(z, 1 +(c(0, y[-1]/100))))
    
    all.equal(z$NewVal, z$NewVal2)
    [1] TRUE
    
    head(z, 10)
        x y   NewVal  NewVal2
    1  25 4 25.00000 25.00000
    2  24 3 25.75000 25.75000
    3  23 0 25.75000 25.75000
    4  22 1 26.00750 26.00750
    5  21 3 26.78773 26.78773
    6  20 2 27.32348 27.32348
    7  19 2 27.86995 27.86995
    8  18 3 28.70605 28.70605
    9  17 4 29.85429 29.85429
    10 16 2 30.45138 30.45138
    

    On my machine, the loop takes just less than 3 minutes to run, while the cumprod statement is virtually instantaneous.

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

Sidebar

Related Questions

I have a data frame with two columns. First column contains categories such as
I have a data frame with 3 variables: place, time, and value (P, T,
I have a data frame that is 100 X 100. There are 30 columns
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data frame where one particular column has a set of specific
I have a data.frame from this code: my_df = data.frame(read_time = c(2010-02-15, 2010-02-15, 2010-02-16,
I have data frame with some numerical variables and some categorical factor variables. The
I have a data.frame called series_to_plot.df which I created by combining a number of
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a data.frame: df<-data.frame(a=c(x,x,y,y),b=c(1,2,3,4)) > df a b 1 x 1 2 x

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.