[R] For Loops please help!!

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Sep 5 18:13:39 CEST 2015


This is not true. The steep price has to do with memory use patterns like result <- c( result, new value ). Vectorization is cleaner, easier to read, and somewhat faster, but for loops are not the monster that they have a reputation for being if the memory is allocated before the loop and elements are updated in the loop. In particular the more complicated the algorithm inside the loop, the less incremental overhead the for loop will introduce.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On September 5, 2015 8:31:15 AM PDT, Dan D <ddalthorp at usgs.gov> wrote:
>Also, any time you write "for" in R, you pay a steep price in
>performance. In
>a short, simple loop it may not be noticeable, but in a more
>challenging
>problem it can be a huge issue.
>
>A more efficient way to write your loop would be:
>infectrate = 400*1.1^(1:30) # calculation
>cbind(1:30,log(infectrate))    # display
>
>-Dan
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711885.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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