Vijaya Kumar Regati
2017-May-30 07:02 UTC
[R] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion
Hi, 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.,). Thanks, Vijay 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]]
Manjusha Joshi
2017-May-30 08:12 UTC
[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> wrote:> Hi, > > 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. t(apply,df,1,cumsum) will give you the required results. Please check help of 'apply' for more details. Best wishes, ?> > ? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Manjusha S. Joshi Mobile: 09822 319328 Pune, India blog:http://manjushajoshi.wordpress.com/ [[alternative HTML version deleted]]
Rolf Turner
2017-May-30 08:44 UTC
[R] [FORGED] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion
On 30/05/17 19:02, Vijaya Kumar Regati wrote:> Hi, > > 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.,).(1) Learn some R (read "An Introduction to R" from the R web page; manuals). Don't use iteration when you don't need to; i.e. use vectorised operations whenever possible. (Much faster and clearer.) (2) Distinguish between data frames and matrices; they are *NOT* the same thing! What you need here are matrices. (3) I think this will work for you: M <- as.matrix(test) Mnew <- cbind(0,M[,-ncol(M)]) + M cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Vijaya Kumar Regati
2017-May-30 08:44 UTC
[R] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion
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: Hi, 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. t(apply,df,1,cumsum) 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 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. -- Manjusha S. Joshi Mobile: 09822 319328 Pune, India blog:http://manjushajoshi.wordpress.com/ 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]]