[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
>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
>View this message in context:
>Sent from the R help mailing list archive at Nabble.com.
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list