Re: [R] how to store recursive results

2006-09-22 Thread Bálint Czúcz
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 <[EMAIL PROTECTED]> 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@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.
>
>
>

__
R-help@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.


Re: [R] how to store recursive results

2006-09-22 Thread Gabor Grothendieck
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 <[EMAIL PROTECTED]> 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@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.
>
>
>

__
R-help@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.


Re: [R] how to store recursive results

2006-09-22 Thread X.H Chen

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 <[EMAIL PROTECTED]>
To: "X.H Chen" <[EMAIL PROTECTED]>
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@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.




__
R-help@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.


Re: [R] how to store recursive results

2006-09-22 Thread X.H Chen

Hi Gabor,

Thanks for pointing out this for me. However, what I try to get is how to 
construct such form a function f that


ret<-f(...),

where ret contains the each recursive result from f, and meantime f consists 
of no <<- operator. Do you have any idea how to implemet this. Thanks a lot 
for your suggestions.


Cheer

Xiaohui Chen

Dept. of Statistics
UBC, Canada





From: "Gabor Grothendieck" <[EMAIL PROTECTED]>
To: "X.H Chen" <[EMAIL PROTECTED]>
CC: r-help@stat.math.ethz.ch
Subject: Re: [R] how to store recursive results
Date: Fri, 22 Sep 2006 06:49:22 -0400

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 <[EMAIL PROTECTED]> 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@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.





_
Buy what you want when you want it on Sympatico / MSN Shopping

__
R-help@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.


Re: [R] how to store recursive results

2006-09-22 Thread X.H Chen

Hi Bálint,

Thanks very much for your suggestions. The party package is a little bit 
complicated to use. Do you have a much simpler example for this problem? 
Anyway, I am gonna try this package.


Cheers,

Xiaohui Chen

Dept. of Statistics
UBC, Canada





From: "Bálint Czúcz" <[EMAIL PROTECTED]>
To: "X.H Chen" <[EMAIL PROTECTED]>
CC: r-help@stat.math.ethz.ch
Subject: Re: [R] how to store recursive results
Date: Fri, 22 Sep 2006 12:21:02 +0200

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 <[EMAIL PROTECTED]> 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@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.





__
R-help@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.


Re: [R] how to store recursive results

2006-09-22 Thread Gabor Grothendieck
1. The point is that you can use <<- and still not pollute the
global environment with any variables so its not clear
why there should be any requirement not to use it.

Other possiblities are

2. to pass the info around through the return value or through an environment:

fact <- function(n) {
if (n == 1) list(n, c("1" = 1))
else {
f <- fact(n-1)
out <- list(n * f[[1]], unlist(f))
names(out[[2]])[1] <- n
out
}
}

fact(4)

3. or through an environment:

fact <- function(n, e) {
if (n == 1) e[["1"]] <- 1
else n * (e[[format(n)]] <- fact(n-1, e))
}

e <- new.env()
fact(4, e)
as.list(e)


On 9/22/06, X.H Chen <[EMAIL PROTECTED]> wrote:
> Hi Gabor,
>
> Thanks for pointing out this for me. However, what I try to get is how to
> construct such form a function f that
>
> ret<-f(...),
>
> where ret contains the each recursive result from f, and meantime f consists
> of no <<- operator. Do you have any idea how to implemet this. Thanks a lot
> for your suggestions.
>
> Cheer
>
> Xiaohui Chen
>
> Dept. of Statistics
> UBC, Canada
>
>
>
>
> >From: "Gabor Grothendieck" <[EMAIL PROTECTED]>
> >To: "X.H Chen" <[EMAIL PROTECTED]>
> >CC: r-help@stat.math.ethz.ch
> >Subject: Re: [R] how to store recursive results
> >Date: Fri, 22 Sep 2006 06:49:22 -0400
> >
> >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 <[EMAIL PROTECTED]> 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@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.
> >>
> >>
> >>
>
> _
> Buy what you want when you want it on Sympatico / MSN Shopping
> http://shopping.sympatico.msn.ca/content/shp/?ctId=2,ptnrid=176,ptnrdata=081805
>
>

__
R-help@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.


Re: [R] how to store recursive results

2006-09-22 Thread Thomas Lumley
On Fri, 22 Sep 2006, X.H Chen wrote:

> Hi Gabor,
>
> Thanks for pointing out this for me. However, what I try to get is how to 
> construct such form a function f that
>
> ret<-f(...),
>
> where ret contains the each recursive result from f, and meantime f consists 
> of no <<- operator. Do you have any idea how to implemet this. Thanks a lot 
> for your suggestions.
>

It depends on the situation. You can always pass the results back in a 
list or vector, eg

cumfactorial<-function(n){
   if (n==0)
   1
else c(1, n*cumfactorial(n-1))
}


If you want to get the results out then you have to either accumulate and 
return them like this or use <<-, since return() and <<- are the only ways 
to get results out of a function.  As long as you don't use <<- to assign 
to variables outside the function it is a perfectly reasonable thing to do

If you were doing something like a Fibonacci sequence then assigning would 
be preferable

 fib<-function(n){
 memo<-new.env(hash=TRUE)
 fibrec<-function(m){
   if (m<=2) return(1)
   vm<-paste("v",m,sep="")
  if(exists(vm,envir=memo,inherits=FALSE))
   return(get(vm,envir=memo,inherits=FALSE))
   rval<-fibrec(m-1)+fibrec(m-2)
  assign(vm,rval,envir=memo)
   rval
}
   fibrec(n)
   sapply(ls(envir=memo),get, envir=memo)
  }

since the memoization converts the algorithm from exponential time to 
linear time.

__
R-help@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.