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

Ranjan Maitra maitra.mbox.ignored at inbox.com
Sat Jul 23 13:55:11 CEST 2016


Hi,

The apply here does exactly what you would expect. (Your problem seems to be with the function that you have written. For some reason, you want to change the values of path2 but are passing it as a variable o a function. The global value of path2 will not change.) For that, you have to also return path2. 

Try:

path1 <- matrix(c(1,2,3,4,5), ncol=1);
path2 <- matrix(c(1,2,3,4,5,6), ncol=1);
apply(X = path1, MAR = 2, function(x, path2 = path2){
    tmp <- x*path2[x+1]
    path2[x+1] <- tmp
    list(tmp, path2)
}, path2 = path2)


Btw, it is good for your sanity to make the distinction between assignments and arguments, and also name your arguments for your function when you call them. I have done that. It makes understanding your code at a glance easier. 

As a personal preference, I have no idea what this is supposed to do, but you may be better off avoiding apply here. I am always nervous about passing arguments such as you do with the x in the apply function. But it is correct, and hopefully does what you want. so I will say nothing more.

HTH!

Best wishes,
Ranjan


On Fri, 22 Jul 2016 18:16:18 -0500 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.

-- 
Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. Please respond to the mailing list if appropriate. For those needing to send personal or professional e-mail, please use appropriate addresses.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!



More information about the R-help mailing list