[R] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion

Vijaya Kumar Regati VijayaKumar.Regati at m3bi.com
Tue May 30 10:44:29 CEST 2017

Hi Manjusha,

Thank you for the response. That surely helps.

But the major issue in my case is not how we can apply sum fn, but how we can  change field names based on iteration number.

I cant skip loop in my case, because in real situation I have to deal with say around 100 fields, the only thing that changes is Day number in field name and the iteration number should be able to handle that.

With Regards,
Vijaya Kumar Regati
Technical Lead, M3bi India Private Ltd
Work: 040-67064732

From: Manjusha Joshi <manjusha.joshi at gmail.com>
Sent: Tuesday, May 30, 2017 1:42:10 PM
To: Vijaya Kumar Regati
Cc: r-help at R-project.org; vijaykr.sas at gmail.com
Subject: Re: [R] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion

Hello Vijaya,

On Tue, May 30, 2017 at 12:32 PM, Vijaya Kumar Regati <VijayaKumar.Regati at m3bi.com<mailto:VijayaKumar.Regati at m3bi.com>> wrote:

I am new to R programming, I am trying to work on below requirement. But could not achieve desired result.
Appreciate if someone can help me on this :

test dataframe :
  Day1.balc Day2.balc Day3.balc Day4.balc
x       100        20        30        40
y       100        10        10        10
> class(test)
[1] "data.frame"

My Goal is to accomplish :
Day2.balc <- Day2.balc + Day1.balc
Day3.balc <- Day3.balc + Day2.balc
Day30.balc <- Day30.balc + Day29.balc

    # Testing for first 4 days
    for (i in 1:4 ) {
    test$Day[i].balc <- test$Day[i].balc + test$Day[i-1].balc

I identified the line I have written inside the loop is not the correct one, can someone help me how I can use iteration value(i), for every iteration, as a basis for changing field names since field consists of 1,2,3... for each different day( Day1.balc Day2.balc Day3.balc Day4.balc etc.,).

​There are many built in functions in R which will help to avoid loops.
Try 'cumsum' to add entries in a row to get cumulative sum. Use 'apply' to avoid loops.

df is the dataframe in which your data has been stored.


will give you the required results.
Please check help of  'apply' for more details.
Best wishes,



R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Manjusha S. Joshi
Mobile:  09822 319328
Pune, India

Disclaimer: IMPORTANT NOTICE: This e-mail (including any attachments) are confidential, may contain proprietary or privileged information and is intended for the named recipient(s) only. If you are not the intended recipient, any disclosures, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this email in error, please notify the sender by return e-mail or telephone immediately and permanently delete the message and any attachments.

	[[alternative HTML version deleted]]

More information about the R-help mailing list