[R] Improving performance by rewriting for loops into apply functions

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Jul 23 08:22:02 CEST 2016


If you complain to the doctor that it hurts when you ram your head into the wall, (s)he is going to tell you to not so that. What do you expect us to say? 

You seem full of misinformation. The apply family functions do not necessarily speed anything up... they are just more compact than for loops.

And yes, their arguments are immutable... as are pretty much all arguments to functions other than environments. So your screwdriver is not going to work on that nail.

Also, working with N x 1 matrices is silly... this is not Matlab.

In many cases you can restructure the problem so that you don't need incremental calculation... but I am not really sure what you wanted in this case. If this were a for loop the code you have looks to me like it would produce

path1 <- c(1,2,3,4,5)
path2 <- c(1,2,3,4,5,6)
path1 * path2[ -1 ]

Maybe it is time for you to read "The R Inferno"? 

Also please read the Posting Guide, which warns you to post plain text (so your email does not get corrupted when it is stripped by the mailing list).
-- 
Sent from my phone. Please excuse my brevity.

On July 22, 2016 4:16:18 PM PDT, "Aleš Grm" <grm.ales at gmail.com> wrote:
>Hello,
>
>I have a slight performance issue that I'd like to solve by rewriting a
>short bit of code that uses for loops so that it would use apply in
>order
>to get some performance gains. My problem is that I can't modify the
>variables that are passed to apply function during apply functions
>execution and use it's latest results. My first thought was to use
>apply
>functions but if this isn't possible I'm open to other suggestions.
>
>For example:
>path1 = matrix(c(1,2,3,4,5), ncol=1);
>path2 = matrix(c(1,2,3,4,5,6), ncol=1);
>apply(path1, 2, function(x, path2){
>  tmp = x*path2[x+1];
>  path2[x+1] = tmp;
>  return(tmp);
>}, path2)
>
>In the code above, path2 should have its elements updated in the course
>of
>the apply function and its value should be use in the next iteration
>while
>executing apply function but that doesn't happen. It seems that when a
>variable is passed into apply function (path2) it is immutable.
>
>BR Aleš
>
>	[[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