[R] Subtraction loop

Jonas Mandel jonas.mandel at curie.fr
Mon Jun 28 17:39:02 CEST 2010


Hello,

Check the function diff() it can do it for you, no need for a loop.

Cheers,
Jonas Mandel


ecvetano at uwaterloo.ca a écrit :
> I have a data frame with 2 columns, one for day and one for average. The
> day starts at 97 all the way to 279. I want to subtract day 98 average-
> day 97 average, then day99 average -day 98 average and so on down my
> list, creating another column with the subtracted results.
> I have:
> 
>     Day DailyAverage
> 1    97    0.6076782
> 2    98    0.7121478
> 3    99    0.8059347
> 4   100    0.9545806
> 5   101    1.0589791
> 6   102    1.1335981
> 7   103    1.2676922
> 8   104    1.4429847
> 9   105    1.6477266
> 10  106    1.7970784
> etc. with values to day 279
> 
> I'm guessing I need to create some kind of loop for this, but I am
> unsure as to how to do this. Any help is greatly appreciated.
> 
> Thanks,
> Emilija
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>



More information about the R-help mailing list