[R] how to store recursive results

X.H Chen xchen_stat at hotmail.com
Fri Sep 22 17:14:41 CEST 2006


Hi Patrick,

Thanks for your suggestion. I find your method works for the functions with 
integer paramters. For example,

If we have function f:
f<-function(i)
{
	if(i>1)
		i*f(i-1)
	else
		1
}

and then using:

ans <- vector("list", n)
for(i in 1:5) {
ans[[i]] <- f(i)
}

the ans should be:
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 6

[[4]]
[1] 24

[[5]]
[1] 120

But actually, we there is no such a "i" can be referrenced in f(), no 
parametric function for example, this will be a problem. Anyway, thanks a 
lot for your suggestions.

Cheers,

Xiaohui Chen

Dept. of Statistics
UBC, Canada




>From: Patrick Burns <pburns at pburns.seanet.com>
>To: "X.H Chen" <xchen_stat at hotmail.com>
>Subject: Re: [R] how to store recursive results
>Date: Fri, 22 Sep 2006 10:26:35 +0100
>
>It isn't clear to me exactly what you are asking, but
>I think that a list might be what you are after. Something
>like:
>
>ans <- vector("list", n)
>for(i in 1:n) {
>ans[[i]] <- ....
>}
>
>X.H Chen wrote:
>
>>Hi all,
>>
>>How to store recursive resutls from a function for each step without using 
>>global operators <<-? Thanks ahead.
>>
>>Xiaohui Chen
>>
>>Dept. of Statistics
>>UBC, Canada
>>
>>_________________________________________________________________
>>Don’t waste time standing in line—try shopping online. Visit Sympatico / 
>>MSN
>>
>>------------------------------------------------------------------------
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch 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