相关文章推荐
活泼的硬盘  ·  matplotlib ...·  1 年前    · 
曾深爱过的电池  ·  html - Get and return ...·  1 年前    · 
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

Warning in dist(mat, method = distance) : NAs introduced by coercion Error in hclust(d, method = method): NA/NaN/Inf in foreign function call (arg 10)

Ask Question

I am trying to generate a heatmap using the function pheatmap(). But I get an error saying there are NA/NaN/Inf in the dataframe. I checkt it but there are no NA/NaN/Inf values in it.. This function worked before, so I do not see what I am doing wrong... Anyone any advise?

heat<-pheatmap(mat, kmeans_k = NA, breaks = NA, border_color= "grey60",cellwidth = 5, cellheight = 0.05, scale = "none", cluster_rows = TRUE,
         cluster_cols = TRUE, clustering_distance_rows = "euclidean",
         clustering_distance_cols = "euclidean", clustering_method = "complete",
         cutree_rows = 1, cutree_cols = 1,
         legend_labels = NA, annotation_row = NA, annotation_col = pd,
         annotation = pd, annotation_colors = anno_colors, annotation_legend = TRUE,
         annotation_names_row = TRUE, annotation_names_col = TRUE,
         drop_levels = TRUE, show_rownames = F, show_colnames = T, main = NA, fontsize = 4,
         fontsize_row = 4, fontsize_col = 4,
         display_numbers = F, number_format = "%.2f", number_color = "grey30",
         fontsize_number = 0.8 * fontsize, gaps_row = NULL, gaps_col = NULL,
         labels_row = NULL, labels_col = NULL, filename = NA, width = NA,
         height = NA, silent = FALSE)
Warning in dist(mat, method = distance) : NAs introduced by coercion
Error in hclust(d, method = method) : 
  NA/NaN/Inf in foreign function call (arg 10)
                You warning, then error, isn't saying the data.frame contains a 'NA', but the process of computing dist introduced NAs, that they then caused hclust to error. You could debugonce(pheatmap) and walk through to the dist process and see what fatal combination in your data is producing the NA. dist and look at the Value for how calculated as clue.
– Chris
                May 17, 2022 at 14:36

One of the probable causes of these error and warnings is the presence of a non-numeric vector in the matrix mat.

For example, the first four columns in the iris data frame are numeric vectors, and the fifth one is a character vector. If you include all the columns as the input for pheatmap, the error and the warning message will pop up:

pheatmap(as.matrix(iris)) # failed.
#Error in hclust(d, method = method) : 
#  NA/NaN/Inf in foreign function call (arg 10)
#In addition: Warning messages:
#1: In dist(mat, method = distance) : NAs introduced by coercion
#2: In dist(mat, method = distance) : NAs introduced by coercion

If you include only the numeric columns, the function is executed with no error or warning

pheatmap(as.matrix(iris[,1:4]))  # succeeded.
                Could you please share your code that you run and the error message that you got? I believe we'll get useful hints from there.
– Abdur Rohman
                Jun 1, 2022 at 21:49
        

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.