當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 lchoose()用法及代碼示例


R語言中的lchoose()函數用於返回nCr值的自然對數。此函數等於 log(choose(x))。

用法: lchoose(n, r)

參數:
n:元素數量
r:組合數

範例1:


# R program to illustrate
# lchoose function
  
# Calling lchoose() function
answer1 <- lchoose(3, 2)  
answer2 <- lchoose(2, 2)   
answer3 <- lchoose(7, 3) 
    
print(answer1)  
print(answer2)  
print(answer3)  

輸出:



[1] 1.098612
[1] 0
[1] 3.555348

範例2:


# R program to illustrate
# lchoose function
   
# Calling lchoose() function which 
# is equivalent to log(choose(x))
answer1 <- log(choose(5, 1))  
answer1_2 <- lchoose(5, 1)
answer2 <- log(choose(4, 2))   
answer2_2 <- lchoose(4, 2)
  
# Printing values
print(answer1)  
print(answer1_2)  
print(answer2)  
print(answer2_2)  

輸出:

[1] 1.609438
[1] 1.609438
[1] 1.791759
[1] 1.791759

這裏,在上麵的代碼中,輸出log(choose())lchoose()是相等的,這表明兩個函數是等價的。




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Calculating Natural Logarithm of calculated nCr value in R Programming – lchoose() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。