Alan Lue
2010-May-26 06:43 UTC
[R] Avoiding Loops When Iterating Over Statement That Updates Its Input
Since `for' loops are slow in R, and since `apply' functions are faster, I was wondering whether there were a way to use an apply function?or to otherwise avoid using a loop?when iterating over a statement that updates its input. For example, here's some such code: r.seq <- 2 * (1 / d$Dt[1] - 1) for (i in 2:nrow(d)) { rf <- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) r.seq <- append(r.seq, rf$root) } The call to `uniroot()' both updates `r.seq' and reads it as input. We could save the output of each invocation of `uniroot()' and concatenate it later, but is there a better way to write this (i.e., to execute more quickly) while updating `r.seq' in each iteration? Alan
Alan Lue
2010-May-26 06:52 UTC
[R] Avoiding Loops When Iterating Over Statement That Updates Its Input
Come to think of it, we can't save the output of each invocation and concatenate it later, since we need the output as input for the next iteration. Alan On Tue, May 25, 2010 at 11:43 PM, Alan Lue <alan.lue at gmail.com> wrote:> Since `for' loops are slow in R, and since `apply' functions are > faster, I was wondering whether there were a way to use an apply > function?or to otherwise avoid using a loop?when iterating over a > statement that updates its input. > > For example, here's some such code: > > r.seq <- 2 * (1 / d$Dt[1] - 1) > for (i in 2:nrow(d)) { > ?rf <- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) > ?r.seq <- append(r.seq, rf$root) > } > > The call to `uniroot()' both updates `r.seq' and reads it as input. > We could save the output of each invocation of `uniroot()' and > concatenate it later, but is there a better way to write this (i.e., > to execute more quickly) while updating `r.seq' in each iteration? > > Alan >-- Alan Lue Master of Financial Engineering UCLA Anderson School of Management
Dennis Murphy
2010-May-26 08:01 UTC
[R] Avoiding Loops When Iterating Over Statement That Updates Its Input
Hi: On Tue, May 25, 2010 at 11:43 PM, Alan Lue <alan.lue@gmail.com> wrote:> Since `for' loops are slow in R, and since `apply' functions are > faster, I was wondering whether there were a way to use an apply > function—or to otherwise avoid using a loop—when iterating over a > statement that updates its input. >That's not necessarily true. Loops that accumulate memory by copying and recopying objects will slow things down, but apply functions execute loops internally at the C level of code. If you loop efficiently in R, it can be as fast or faster than a given apply family function.> For example, here's some such code: > > r.seq <- 2 * (1 / d$Dt[1] - 1) > for (i in 2:nrow(d)) { > rf <- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) > r.seq <- append(r.seq, rf$root) > } > > The call to `uniroot()' both updates `r.seq' and reads it as input. > We could save the output of each invocation of `uniroot()' and > concatenate it later, but is there a better way to write this (i.e., > to execute more quickly) while updating `r.seq' in each iteration? >I would look into the Vectorize() function for this task. HTH, Dennis> > Alan > > ______________________________________________ > 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]]