Aleš Grm
2016-Jul-22 23:16 UTC
[R] Improving performance by rewriting for loops into apply functions
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]]
Jeff Newmiller
2016-Jul-23 06:22 UTC
[R] Improving performance by rewriting for loops into apply functions
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.
Ranjan Maitra
2016-Jul-23 11:55 UTC
[R] Improving performance by rewriting for loops into apply functions
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!