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
One suggestion: as a recursive list. For example have a look at the 'party' package and the BinaryTree class. I hope this helps. B?lint On 22/09/06, X.H Chen <xchen_stat at hotmail.com> 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. > > >
Note that <<- is not necessarily global: if (exists("x")) rm(x) f <- function() { x <- 2 g <- function() x <<- 3 g() x } f() # 3 exists("x") # FALSE On 9/22/06, X.H Chen <xchen_stat at hotmail.com> 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. > > >
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. >> >>