2011-08-19 81 views
2

當我使用hclust函數時出現以下錯誤?我怎麼解決這個問題? 我使用Windows 7和2.12.3版本R.R中的分層聚類

n_seq <- 250             
mat <- matrix(NA, ncol=n_seq, nrow=n_seq)  
for (idx in 1:n_seq) 
{mat[idx,idx] <- 0.0}   
for(idx in 1:(n_seq-1)) 
{intemp <- read.xls("C:// clustal.xls", sheet = idx); 
mat[(1+idx):n_seq,idx] <- intemp[1:(n_seq-idx), 11]} 

fit <- hclust(as.dist(mat), method="single") 

Error in hclust(as.dist(mat), method = "single") : 
NA/NaN/Inf in foreign function call (arg 11) 

請幫我解決這個問題。

+3

請做一個可重現的例子(見這篇文章的提示:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – nullglob

+0

正如nullglob所說。順便說一句,再讀一遍關於nullglob給你的鏈接的答案。運行你的代碼是不可能的,所以這不是一個可重複的例子。 –

+0

以進行更多調試:嘗試使用'cat'打印索引編號,以便您輕鬆通過,以便您輕鬆分辨哪個表可以解決問題;然後使用'which(!is.finite(mat),arr.ind = TRUE)'來查看距離矩陣中的哪些元素是有問題的 –

回答

4

出現此錯誤消息是因爲距離矩陣as.dist(mat)的值不正確(NA,NaNInf)。如果你看hclust的代碼,外部函數調用的第11個參數(即通過編譯代碼)就是距離矩陣的值,這就是R所抱怨的。默認情況下,外部函數調用不接受NA,NaNInf。有關更多信息,請參閱?.Fortran