'How can I write a recursive function in R?

How can I write a recursive function to obtain the combination(n,r) = combination(n-1, r-1) + combination(n-1,r) in R? I tried the following code but I only get an error message:

nCr=function(n, r) {
if (r == 0)
{
if (r == n) {
    
return (1)
} } else {
return (nCr(n-1, r-1) + nCr(n-1, r)) 
}
}

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source