2011-08-19 80 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