Heatmap Error: "X" must be a numeric matrix

I'm trying to create a heatmap with dendrograms and I keep getting this error: Error in heatmap: "x" must be a numeric matrix. My data table is comparing the deltact for each specific gene (columns) amongst each subjectID that's on the lefthand side.

Here is my csv data table:

COX2 S100A7 LMNB1 CCL5 CXCL10 OXTR FLG KLK6 HMOX1 S100A2
01-0071 10.6
01-0081 2.4 8.57
01-0129 0
01-0169 7.05 4.63
01-0277 5.23 10.8 2.41 3.57 0
01-0281 6.8 3.12 3.38 5.96 7.02
01-0288 0
01-0476 5.17
02-0017 5.13 8.41
02-0023 6.2
02-0056 5.13
02-0069 0
02-0072 3.92
02-0100 2.47
02-0139 0
02-0170 6.98
03-0068 0
03-0076 4.53 6.24
03-0080 6.2 6.26
03-0083 4.71 4.27
03-0091 8.27 4.38 4.11 6.71
03-0116 5.63
03-0131 8.95
03-0150 2.69 9.74
03-0150 0
03-0162 4.12 2.75 4.04 5.85 6.18
03-0221 0
03-0287 6.38 0 0
03-0297 0
03-0355 3.61 7.29
03-0386 11.2
03-0390 4.91 4.91
03-0399 0
04-0027 3.3 6.39
04-0029 4.72
04-0052 9.7
04-0060 5.19
04-0060 0
04-0061 7.47
04-0062 6.98
05-0008 4.76
05-0010 3.47 7.66
05-0013 2.59 5.9
05-0015 5.92 3.5
05-0017 4.51
05-0018 2.39 6.13 4.63
05-0033 5.59
05-0038 3.45 7.15
05-0046 6.75 11.1 2.5 6.32
05-0078 2.91
05-0094 8.32 0
06-0071 4.6 10.7 3.17 0
06-0073 6.07
06-0080 5.36 3.99
06-0094 2.17
06-0119 2.31 3.38 5.91
07-0001 6.34
07-0037 2.85
08-0035 6.75 3.06 7.6
08-0202 4.92 9.3 3.62 4.67 5.93 4.37 5.86 0
08-0215 7.08
08-0217 5.94 2.99
08-0274 3.74
08-0278 6.57 10.2 3.94 6.68 6.44
09-0088 10.7
09-0155 3.39 6.97
09-0177 0
09-0179 0
09-0201 3.35 7.57
09-0246 0
09-0249 0
09-0255 4.75 8.69
10-0036 5.65 7.6
10-0040 7.52 4.63
11-0063 3.69 7.34
12-0047 3.21
13-0009 6.15
13-0038 0
13-0047 0

It would help if you provided the code you're using. Trying out something:

# Read in the data I copy-pasted from your post
tab <- read.csv("table.csv")

class(tab)
#> [1] "data.frame"
# take a look at first few rows
head(tab)
#>         X COX2 S100A7 LMNB1 CCL5 CXCL10 OXTR  FLG KLK6 HMOX1 S100A2
#> 1 01-0071   NA   10.6    NA   NA     NA   NA   NA   NA    NA     NA
#> 2 01-0081   NA     NA  2.40   NA     NA   NA   NA   NA  8.57     NA
#> 3 01-0129   NA     NA    NA   NA     NA   NA   NA   NA    NA      0
#> 4 01-0169   NA     NA    NA   NA     NA   NA 7.05   NA  4.63     NA
#> 5 01-0277 5.23   10.8  2.41 3.57     NA   NA   NA   NA    NA      0
#> 6 01-0281 6.80     NA  3.12 3.38     NA 5.96 7.02   NA    NA     NA

# Convert to matrix (make sure to keep the first column as rownames)
mat <- as.matrix(tab[,-1])
rownames(mat) <- tab[,1]

# look at top-left corner
mat[1:3,1:3]
#>         COX2 S100A7 LMNB1
#> 01-0071   NA   10.6    NA
#> 01-0081   NA     NA   2.4
#> 01-0129   NA     NA    NA

class(mat)
#> [1] "matrix" "array"
typeof(mat)
#> [1] "double"


# try plotting it without dendrogram
image(mat)


# this doesn't work because of the many NA
heatmap(mat)
#> Error in hclustfun(distfun(x)): NA/NaN/Inf in foreign function call (arg 10)
pheatmap::pheatmap(mat)
#> Error in hclust(d, method = method): NA/NaN/Inf in foreign function call (arg 10)


mat[is.na(mat)] <- 0
# take a look
mat[1:3,1:3]
#>         COX2 S100A7 LMNB1
#> 01-0071    0   10.6   0.0
#> 01-0081    0    0.0   2.4
#> 01-0129    0    0.0   0.0

# Now we can plot it!
heatmap(mat)

pheatmap::pheatmap(mat)

Created on 2022-09-25 by the reprex package (v2.0.1)

Here I set all the NA to 0 to make distance computation and clustering possible, in your particular case you have to see if it's the right thing to do.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.