[R] About 'choose' function

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Nov 8 14:17:19 CET 2004


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?

prod() calculates the whole product while choose() can do it the clever 
way avoiding overflows. On my machine, .Machine$double.xmax is 
1.797693e+308, hence prod(1:170) works while prod(1:171) is too much to 
be represented correctly ...

Uwe Ligges


> 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