heatmap error when implementing as.matrix

Viewed 98

When I use heatmap function to make heatmap of dataset, I get an error, I tried:

df1$family <- substr(as.character(df1$gene_id), 1, nchar(as.character(df1$gene_id))-2)
df01<-df1$family
df01m<-as.matrix(df01)
heatmap(df01m)

I get this error:

Error in heatmap(df01m): 'x' must be a numeric matrix
Traceback:

1. heatmap(df01m)
2. stop("'x' must be a numeric matrix")

The dataset is big, so I cut some of it:

structure(list(gene_id = structure(6:11, .Label = c("__alignment_not_unique", 
"__ambiguous", "__no_feature", "__not_aligned", "__too_low_aQual", 
"ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", 
"ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", 
"ENSG00000001167", "ENSG00000001460", "ENSG00000001461", "ENSG00000001497", 
"ENSG00000001561", "ENSG00000001617", "ENSG00000001626", "ENSG00000001629", 
"ENSG00000001630", "ENSG00000001631", "ENSG00000002016", "ENSG00000002079", 
"ENSG00000002330", "ENSG00000002549", "ENSG00000002586", "ENSG00000002587", 
"ENSG00000002726", "ENSG00000002745", "ENSG00000002746", "ENSG00000002822", 
"ENSG00000002834", "ENSG00000002919", "ENSG00000002933", "ENSG00000003056", 
"ENSG00000003096", "ENSG00000003137", "ENSG00000003147", "ENSG00000003249", 
"ENSG00000003393", "ENSG00000003400", "ENSG00000003402", "ENSG00000003436", 
"ENSG00000003509", "ENSG00000003756", "ENSG00000003987", "ENSG00000003989", 
"ENSG00000004059", "ENSG00000004139", "ENSG00000004142", "ENSG00000004399", 
"ENSG00000285989", "ENSG00000285990", "ENSG00000285991", "ENSG00000285992", 
"ENSG00000285993", "ENSG00000285994"), class = "factor"), expr = c(6L, 
754L, 447L, 426L, 5L, 1L)), row.names = c(NA, 6L), class = "data.frame")

head of the data set:

        gene_id         expr
           <fct>       <int>
1   ENSG00000000005     6
2   ENSG00000000419     754
3   ENSG00000000457     447
4   ENSG00000000460     426
5   ENSG00000000938     5
6   ENSG00000000971     1
1 Answers

The error shows that we need a numeric matrix. The substr function returns a character string. So, we can convert the substring vector to numeric

df01m <- as.matrix(as.numeric(df01))

Another issue is that heatmap requires a matrix with atleast 2 rows/2 columns. Here the as.matrix converts the vector to a single column matrix and it may not work

Related