Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

pheatmap scale="row" giving Error in hclust(d, method = method) : NA/NaN/Inf in foreign function call

Ask Question

I am trying to create a heatmap with gene expression values with the package pheatmap in R. I have used the code numerous times and never had a problem untill today. It seems when I do the scale="row" , I end up with this error. I cannot create the z-scores. So probably some rows have no variability for which this is happening. How can I get rid of this. The matrix is having 1100 rows and 9 columns. My code:

data  <- read.table("~path/DEGs_DESeq.txt",sep="\t")
data2 <- as.matrix(data[,2:9])
data3 <- data2[-1,]
samples <- data2[1,]
genes <- data[2:length(data2[,1]),1]
vett <- as.numeric(data3)
data4 <- matrix(vett, length(genes), length(samples), dimnames=list(paste(genes),paste(samples))) 
head(data4)
pheatmap(as.matrix(data4), col=bluered(200), scale="row", key=T, keysize=1.5,
    density.info="none", trace="none",cexCol=0.6, fontsize_row=8, fontsize_col=10)

Error in hclust(d, method = method) : NA/NaN/Inf in foreign function call (arg 11)

How can I get rid of this error?

I had the same problem and your comment was very helpful in solving the problem. You should list it as an answer. Also this code is what I used: counts_filtered_df <- counts_df[apply(counts_df, MARGIN = 1, FUN = function(x) sd(x) != 0),] – user5359531 Oct 27, 2016 at 20:58 Please explain to the rest of us who don't have your dataset (so can't reproduce this) why pheatmap(..., scale="row") did not compute some SDs? Was it grouping? Was it groups with a size of one, by any chance? Did your dataset actually have NAs in any rows? Otherwise this question is not a reusable resource. – smci Sep 2, 2018 at 22:49 I'm not the OP, but was having a similar problem. In my dataset, which I'd checked for non-numeric and NA values, the issue was that there were a few rows with the same value in every column. For these, the SD was 0 and this was screwing up the row scaling. Removing these rows solved the problem. – Max F Jun 27, 2020 at 16:21 Thanks for taking the lead Max and addressing the query of the comment. Really appreciate it. I shared the same answer as I solved my own problem. Apologies if it wasn’t clear enough, however Max did clarify it. – ivivek_ngs Jun 28, 2020 at 17:04

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.