Thanks Jeff, and William. @William: your example is terrific, very clear. I love it, thanks!
Cheers, Mike On Sun, Apr 7, 2013 at 7:52 PM, William Dunlap <wdun...@tibco.com> wrote: > > I am aware of the apply() functions, > > but they are wrapper function of for loops, so they are slower. > > While this sounds right in the abstract, it isn't always so. Many times > looping code is slow not because of the function calls of interest but > because of how the memory used to store the result is managed and > other details in setting up the loop. E.g., compare the following loops: > > system.time({ x1 <- numeric(0) ; for(i in 1:50000) x1[i] <- log(i) }) > user system elapsed > 2.34 0.03 2.37 > > system.time(x2 <- sapply(1:50000, log)) > user system elapsed > 0.08 0.00 0.08 > > system.time({ x3 <- numeric(50000) ; for(i in 1:50000) x3[i] <- log(i) > }) > user system elapsed > 0.11 0.00 0.11 > identical() shows that all those give the same results. Using sapply() > means > that you don't have to remember the rule about allocating the output vector > ahead of time, thus simplifying your code. > > The built-in apply functions are also compiled, which saves some time. > You can > compile your own functions: > > library(compiler) > > f5 <- function(n,FUN) { x <- numeric(n) ; for(i in seq_len(n)) x[i] <- > FUN(i) ; x } > > f5_compiled <- cmpfun(f5) > > system.time( x5 <- f5(50000, log) ) > user system elapsed > 0.09 0.00 0.09 > > system.time( x5_compiled <- f5_compiled(50000, log) ) > user system elapsed > 0.02 0.00 0.01 > > The built-in vapply() allows you to specify the type (and size) of the > return value > of FUN, which can save time also (that was also part of the speedup of > f5() above). > > system.time(x6 <- vapply(1:50000, log, FUN.VALUE=0.0)) > user system elapsed > 0.06 0.00 0.06 > vapply() also checks that FUN(X[i]) returns a value of the expected type > and size, > something the others did not do. > > So you can write looping code that faster than the built-in *apply > functions, but > it may be a fair bit more work than using them. > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > > -----Original Message----- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On Behalf > > Of C W > > Sent: Sunday, April 07, 2013 3:11 PM > > To: John Kane > > Cc: r-help@r-project.org; Baylee Smith > > Subject: Re: [R] While loop history > > > > May I say also ask one thing? @OP: sorry to use your post. > > > > What would you use instead of loops? I am aware of the apply() > functions, > > but they are wrapper function of for loops, so they are slower. At one > > point, I was told to go back to C for faster implementation, but I like R > > much more. > > > > In the case of repeated simulation such as Monte Carlo, what would you > use > > instead of for loop? > > > > Mike > > On Sun, Apr 7, 2013 at 7:43 AM, John Kane <jrkrid...@inbox.com> wrote: > > > > > > > > > http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible- > > example > > > > > > Loops are seldom a good solution in R so some more information and data > > > would be useful > > > > > > At the simplist, for your specific question I think you could set up > two > > > vectors (e.g. v1 <- rep(NA, 10 ) and just write the values into the > > > vectors as you proceed through the loop. > > > > > > John Kane > > > Kingston ON Canada > > > > > > > > > > -----Original Message----- > > > > From: bayywa...@gmail.com > > > > Sent: Sun, 7 Apr 2013 14:36:33 +1200 > > > > To: r-help@r-project.org > > > > Subject: [R] While loop history > > > > > > > > Hi, > > > > I am new at R and still trying to get the hang of things. > > > > I am running a while loop and wish to save the results of each > iteration. > > > > The results are a vector x of length two and I wish to save the > results > > > > of > > > > each iteration as two vectors, one for x[1] and the other for x[2]. > > > > > > > > Thanks! > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > > > R-help@r-project.org mailing list > > > > 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. > > > > > > ____________________________________________________________ > > > GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at > > > http://www.inbox.com/smileys > > > Works with AIM(r), MSN(r) Messenger, Yahoo!(r) Messenger, ICQ(r), > Google Talk(tm) and > > > most webmails > > > > > > ______________________________________________ > > > R-help@r-project.org mailing list > > > 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. > > > > > > > [[alternative HTML version deleted]] > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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.