[R] About 'choose' function

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Mon Nov 8 14:30:30 CET 2004


Try with less ambitious numbers such as my.choose1(60,20). I think it
works fine.

I think the problem is that gamma(6001) and prod(1:6000) are so large
that it gives Inf as the answer. Hence the numerator and denominator
approaches Inf and division of two Inf gives NaN.

You could use the natural log version of gamma (or even lchoose) to
handle these large numbers as below 

my.choose3 <- function(x,y){
  y <- lgamma(x+1) - lgamma(y+1) - lgamma(x-y+1)
  return( exp(y) )
}

But have you tested the case when your inputs are not integers ?

Regards, Adai


On Mon, 2004-11-08 at 11:58, John wrote:
> Hello R-users,
> 
> When I didn't know about the internal 'choose'
> function, I made such function, 'my.choose' below. But
> when I used them instead of choose(6000,20), they
> didn't give me any answer.
> 
> What is the difference between 'choose', 'my.choose1',
> and 'my.choose2' below? That is, what is behind
> 'choose' function and what's the problem using 'prod'
> or 'gamma' function?
> 
> Thanks a lot.
> 
> John
> 
> ##########
> 
> > choose(6000,20)
> [1] 1.455904e+57
> >
> > my.choose1 <- function(x,y) {
> prod(1:x)/(prod(1:y)*prod(1:(x-y))) }
> > my.choose1(6000,20)
> [1] NaN
> >
> > my.choose2 <- function(x,y) {
> gamma(x+1)/(gamma(y+1)*gamma(x-y+1)) }
> > my.choose2(6000,20)
> [1] NaN
> >
> 
> ______________________________________________
> 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
>




More information about the R-help mailing list