[R] Saving output in an iterated function
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Jun 21 23:21:33 CEST 2009
Extend it like this:
Try this:
f <- function(d, endPeriod) with(d, {
period <- period + 1
ce <- eh + be
de <- (eh + be)^2
ee <- ifelse(ce > de, de, ce)
eh <- ifelse(period < endPeriod, ce, eh)
data.frame(eh, be, ce, de, ee, period)
})
ones <- c(1, 1, 1)
twos <- c(2, 2, 2)
d <- data.frame(eh = ones, be = twos, period = 0)
Reduce(f, init = d, x = rep(4, 4))
Reduce(f, init = d, x = rep(4, 4), accumulate = TRUE)
On Sun, Jun 21, 2009 at 4:49 PM, Economics Guy<economics.guy at gmail.com> wrote:
> Unfortunately my actual function is a bit more complicated than
> "ce=ce+be" and does many transformations on the original vectors.
> Maybe something like this better conveys what I am trying to do:
>
> functM <- function(eh,be,period,endPeriod){
> period <- period+1
> ce <- eh+be
> de <- (eh+be)^2
> ee <- ifelse(ce>de,de,ce)
> {if (period < endPeriod)
> functM(ce,be,period,endPeriod)
> else data.frame(eh,be,ce,de,ee,period)}
> }
>
> ones <- c(1,1,1)
> twos <- c(2,2,2)
> functM(ones,twos,0,4)
>
>
>
>
> On Sun, Jun 21, 2009 at 10:59 AM, Gabor
> Grothendieck<ggrothendieck at gmail.com> wrote:
>> Try Reduce:
>>
>> f <- function(d, incr = 1) with(d,
>> data.frame(eh = ce, be, ce = ce + be, period = period + incr))
>> d <- data.frame(ce = ones, be = twos, period = 0)
>> Reduce(f, init = d, x = rep(1, 4))
>> Reduce(f, init = d, x = rep(1, 4), accumulate = TRUE)
>>
>>
>> On Sun, Jun 21, 2009 at 9:06 AM, Economics Guy<economics.guy at gmail.com> wrote:
>>> # I have a function that takes a few vectors manipulates them and then
>>> outputs a matrix of results:
>>>
>>> funct1 <- function(eh,be){
>>> ce <- eh+be
>>> data.frame(eh,be,ce)
>>> }
>>>
>>> ones <- c(1,1,1)
>>> twos <- c(2,2,2)
>>> funct1(ones,twos)
>>>
>>>
>>> # I would like to iterate it and save the results from each iteration.
>>> I could of course write a loop but I would like to do this is the most
>>> efficient way possible. The best I have come up with is this:
>>>
>>>
>>> functM <- function(eh,be,period,endPeriod){
>>> period <- period+1
>>> ce <- eh+be
>>> {if (period < endPeriod)
>>> functM(ce,be,period,endPeriod)
>>> else data.frame(eh,be,ce,period)}
>>> }
>>>
>>> ones <- c(1,1,1)
>>> twos <- c(2,2,2)
>>> functM(ones,twos,0,4)
>>>
>>> # As you can see it only reports the results form the last iteration.
>>> Here it is the 4th iteration, but if you change the last argument of
>>> the previous run of functM, you can see that it will always report the
>>> final matrix. How can I have it save/store/write the results of each
>>> iteration. I do not care if it stacks it all into the same matrix or
>>> if I have to write.csv each time. I just want to do it in the most
>>> efficient way possible (the actual program will run for many many
>>> iterations of large vectors.
>>>
>>> # Thanks.
>>>
>>> ______________________________________________
>>> R-help at 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.
>>>
>>
>
More information about the R-help
mailing list