2

I am getting the following error when I use hclust function? How can I solve this problem? I am using windows 7 and 2.12.3 version of 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)

Please help me to solve this problem.

manjitha
  • 21
  • 1
  • 3
  • 3
    Please make a reproducible example (see this post for tips: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – nullglob Aug 19 '11 at 09:19
  • As nullglob said. BTW, read again the answer(s) on the link nullglob gave you. It is impossible to run your code, so that is _not_ a reproducible example. – Joris Meys Aug 19 '11 at 09:52
  • for more debugging: try using `cat` to print out the index numbers as you go through, so you can easily tell which sheet is giving you the problem; then use `which(!is.finite(mat),arr.ind=TRUE)` to see which element(s) in the distance matrix are problematic – Ben Bolker Aug 20 '11 at 15:49

1 Answers1

4

This error message arises because the distance matrix as.dist(mat) has a bad value (NA, NaN or Inf) in it. If you look in the code of hclust, the 11th argument to the foreign function call (i.e. by compiled code) is the values of the distance matrix, and this is what R is complaining about. By default, NA, NaN or Inf are not accepted by foreign function calls. See ?.Fortran for more info.

nullglob
  • 6,903
  • 1
  • 29
  • 31