[R] Need Help - R Programming - Using iteration value to change field names for processing for every iteraion
Manjusha Joshi
manjusha.joshi at gmail.com
Tue May 30 10:12:10 CEST 2017
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]]
More information about the R-help
mailing list