[R] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Wed May 31 08:45:56 CEST 2017
That is not possible... this is just a thread of emails. The closest you can get is to clearly indicate what solution worked for you so that people following along now it in the archives later know how your question was answered.
--
Sent from my phone. Please excuse my brevity.
On May 30, 2017 9:24:45 PM PDT, Vijaya Kumar Regati <VijayaKumar.Regati at m3bi.com> wrote:
>Thanks very much.
>
>Can someone suggest me how I can close this qn, since I have the
>solution now ?
>
>
>With Regards,
>Vijaya Kumar Regati
>Technical Lead, M3bi India Private Ltd
>Work: 040-67064732
>
>________________________________
>From: David L Carlson <dcarlson at tamu.edu>
>Sent: Wednesday, May 31, 2017 3:35:19 AM
>To: Vijaya Kumar Regati; Manjusha Joshi
>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
>
>Refer to columns by position rather than name and everything is
>simpler:
>
>for (i in 2:4 ) {
> test[, i] <- test[, i] + test[, i-1]
>}
>
>Note your approach fails on the first line since you start with i=1 and
>there is no Day0. Another approach that is simpler:
>
>t(apply(test, 1, cumsum))
>
>
>David L. Carlson
>Department of Anthropology
>Texas A&M University
>
>-----Original Message-----
>From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Vijaya
>Kumar Regati
>Sent: Tuesday, May 30, 2017 3:44 AM
>To: Manjusha Joshi <manjusha.joshi at gmail.com>
>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
>
>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]]
>
>______________________________________________
>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.
>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]]
>
>______________________________________________
>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.
More information about the R-help
mailing list