Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heatmap error with : 'x' must be a numeric matrix

Tags:

r

matrix

heatmap

I know this question might be duplicated, but I was trying some of the solutions posted in this forum with no success, and that's why I am posting it here.

Let's start with my dataset to make it reproducible.

dataset <- structure(list(Comparison = c("SH vs SAP", "SH vs NEA", "SH vs ERE", 
"SH vs ERH", "SH vs NAL", "SAP vs NEA", "SAP vs ERE", "SAP vs ERH", 
"SAP vs NAL", "NEA vs ERE", "NEA vs ERH", "NEA vs NAL", "ERE vs ERH", 
"ERE vs NAL", "ERH vs NAL"), DC1 = c(NA, NA, NA, NA, NA, 1, 1, 
1, NA, 1, 1, NA, 1, NA, NA), DC2 = c(NA, NA, NA, NA, NA, 1, 1, 
1, NA, 0, 0, NA, 1, NA, NA), DC3 = c(1, 1, 1, 1, 1, 1, 1, 1, 
0, 1, 0, 0, 1, 0, 1), DC4 = c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 
0, 1, 1, 1), DC5 = c(0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
1), DC6 = c(0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1), DC7 = c(0, 
1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1), DC8 = c(0, 1, 0, 1, 
1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1), DC9 = c(0, 0, 0, 0, 1, 0, 0, 
0, 0, 0, 1, 1, 0, 0, 0), DC10 = c(1, 1, 0, 1, 1, 0, 0, 0, 0, 
0, 1, 0, 1, 0, 0)), .Names = c("Comparison", "DC1", "DC2", "DC3", 
"DC4", "DC5", "DC6", "DC7", "DC8", "DC9", "DC10"), class = "data.frame", row.names = c(NA, 
15L))

I have tried to change the dataset to a matrix, as this been suggested in other posts. However, it keeps giving the same error

heatmap(dataset)
heatmap(as.matrix(dataset))

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

Error in heatmap(as.matrix(dataset)) : 
  'x' must be a numeric matrix

I tried to convert to numeric the columns, but the error keeps. And so is the case when I remove DC1 and DC2 columns which contain NA values.

Any help to spot the problem?

like image 556
antecessor Avatar asked Dec 17 '22 21:12

antecessor


1 Answers

dataset[, 1] is character so as.matrix(dataset) is a character matrix. This explains:

'x' must be a numeric matrix

Your probably want

heatmap(as.matrix(dataset[, -1]))

enter image description here

And how can I include the names of the rows on the right?

Set the Comparison variable as the rownames of the matrix:

m <- as.matrix(dataset[, -1])
rownames(m) <- dataset$Comparison
heatmap(m)

enter image description here

So your real issue is really Convert the values in a column into row names in an existing data frame in R although the problem is presented with heatmap.

like image 85
Zheyuan Li Avatar answered Jan 02 '23 01:01

Zheyuan Li