Error while creating heatmaps - NA/NaN/Inf in foreign function call (arg 11)

本小妞迷上赌 提交于 2019-12-23 09:03:09

问题


I'm trying to prepare heatmap for my data but I have no idea why this error appears.

My data:

> dput(head(tbl_ready))
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.370330677123077, 
0, 0, 0, 0, 0, 0.53318856142826, 0, 0, 0, 0, 0, 0.217669675587482, 
0, 0, 0, 0.79337589572453, 0, 1, 0.0132525790616207, 0, 0, 1, 
0.498415470211292, 0.216961707575178, 0.0646831352678839, 0, 
0, 0, 0.778625047514492, 0.165974546372072, 0.076951015613392, 
0.889894091237216, 0, 0, 1, 0.129806153151281, 0.197647497443337, 
1, 0, 0, 0.509023013860118, 0.159412145987791, 0.207873742711735, 
0.749031133231353, 0.222918051830986, 0, 0.741479370384933, 0.133323148299248, 
0.216599753666685, 0.962652293738836, 0.303065152126049, 0, 0.801394522615822, 
0, 0.15864534869139, 0, 0.193050421324826, 0, 0.799048954936309, 
0, 0.328823938175914, 1, 0.425065664971905, 0, 0.578904125773447, 
0, 0.186228586828205, 0, 0.428641900764779, 0, 0.276948678897629, 
0, 0.117434041208573, 0, 0.272644463294893, 0, 0.138614907082177, 
0, 0.180927300758764, 0, 0.435388112571728, 0, 0, 0, 0.475087644525129, 
0, 0.762104865898499, 0, 0, 0, 0.227200985463951, 0, 0.418218667506089, 
0, 0, 0, 0.478763051110549, 0, 0.875837464800909, 0, 0, 0, 0.188429982762364, 
0, 0.418218667506089, 0, 0, 0, 0.349226871785504, 0, 0.998103388096146, 
0, 0, 0, 0.444963481341964, 0, 1, 0, 0, 0, 0.0670152075162316, 
0, 0, 0, 0, 0, 1, 0, 0, 0), .Dim = c(6L, 24L), .Dimnames = list(
    NULL, c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", 
    "X10", "X11", "X12", "X13", "X14", "X15", "X16", "X17", "X18", 
    "X19", "X20", "X21", "X22", "X23", "X24")))

The code which I use to create a heatmap (it works for other data):

## For all of the genes
tbl_ready <- as.matrix(tbl_for[,2:25])
mode(tbl_ready)<-"numeric"

library(gplots)
heatmap.2(tbl_ready,
          Colv = NA,
          scale = "none",
          dendrogram = "row",
          col=rainbow(256, s = 1, v = 1, start = 0, end = 2/6, alpha = 1),
          density.info = "none",
          trace = "none",
          key = FALSE,
          cexCol=0.5,
          cexRow=1)

mtext(paste("ATh",""), side=3, line=-1, cex=1.5, col="black") 
mtext("Size in kDa", side=1, line=3.5, cex=1.3, col="black")  

I even tried to replace Na/NaN/Inf to 0 to avoid problems:

## Replacing Na by 0
is.na(tbl_ready) <- sapply(tbl_ready, is.infinite)
tbl_ready[is.na(tbl_ready)] <- 0
tbl_ready[is.nan(tbl_ready)] <- 0

but the error still appears:

Error in hclustfun(distfun(x)) : 
  NA/NaN/Inf in foreign function call (arg 11)

回答1:


I too was unable to reproduce the error with the sample data provided above, but when I did

tbl_ready[1,1] <- Inf

and ran it again, I got the same error. This means there is likely an infinite value in the matrix. Are you sure that sum(is.infinite(tbl_ready)) returns 0 after your cleaning?




回答2:


Try d3heatmap(tbl_ready,Rowv = FALSE, Colv=FALSE)

The automatic reordering of rows and columns was causing an issue for me too, disabling it fixed the problem




回答3:


That is happening because you have a blank line somewhere in the table.




回答4:


same error got resolved with the following code

  docs <- dist( as.matrix(dtm), method = "euclidean")
  hclust_dist<- as.dist(docs)
  hclust_dist[is.na(hclust_dist)] <- 0
  hclust_dist[is.nan(hclust_dist)] <- 0
  sum(is.infinite(hclust_dist))  # THIS SHOULD BE 0
  h <- hclust(hclust_dist, "ward.D2")


来源:https://stackoverflow.com/questions/23738900/error-while-creating-heatmaps-na-nan-inf-in-foreign-function-call-arg-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!