0
votes

I would like to perform heatmap. I transferred the data frame to matrix. My first column in the matrix contains 51 state names in character format. Due to this when I execute heatmap an error pops out ('X' must be numeric). If I convert the matrix into numeric all the states get converted to numeric values from 1 to 51. Name of the state gets changed to numbers. I would like someone to help me in converting the character column into numeric without any value change in the column. enter image description here I get the following error:

> heatmap.2(matrix)
Error in heatmap.2(matrix) : `x' must be a numeric matrix

dput(matrix[1:20,1:5])
structure(c("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", 
"FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", 
" 156023.01", " 934292.20", " 565543.16", " 859246.77", "1802826.03", 
" 236048.04", " 277419.16", "  44170.06", " 364245.19", "3059883.80", 
"1032052.28", "  49148.00", " 484355.76", " 103032.97", "1501399.16", 
"1098716.37", " 536964.81", " 714912.96", " 930454.92", "1006184.61", 
NA, " 647281.97", " 243467.03", " 222016.05", "1955376.54", " 284157.80", 
" 546510.14", " 310209.01", " 238855.76", "3055374.94", " 620487.04", 
"  52286.08", " 183689.95", " 101198.95", "2299302.42", " 682522.43", 
" 203429.06", " 566182.29", " 434137.97", "1269701.60", "  279984.88", 
" 1785117.72", " 1210217.08", " 1738388.11", "12313826.52", " 1033786.31", 
" 1905870.34", " 1589936.20", " 1177198.27", " 7379680.11", " 3182089.09", 
"  539865.15", "  907408.47", "  706547.91", " 5616722.28", " 2793763.32", 
"  751262.24", " 2620593.80", " 3327343.31", " 3423941.61", "  277346.4", 
" 3231424.9", " 1784411.7", " 2539940.3", "13107647.6", " 1623508.4", 
" 2475804.7", " 1382151.2", " 1362240.3", "10431341.9", " 4514651.7", 
" 1081821.1", " 1653629.7", "  594605.5", " 9147134.3", " 4121661.9", 
" 1292330.2", " 3252592.8", " 3360762.2", " 4269284.1"), .Dim = c(20L, 
5L), .Dimnames = list(NULL, c("Provider.State", "039 ", "057 ", 
"064 ", "065 ")))
2
This sounds like you have factors which are being converted to their integer indices before being convert to numeric. Try as.numeric(as.character(...)) and see if it changes. - r2evans
It doesn't work. It changes the entire column to NA if I perform it in data frame. In matrix the value gets changed. - Anjan Karpak
It would help to have a reproducible question. This includes sample code (including listing non-base R packages), sample data (e.g., dput(head(x))), and you've stated that your expected output is a heatmap. Refs: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info. - r2evans
I still could not able to find the answer for my ques even after having a look at the links. Can u help me in providing d code for it? - Anjan Karpak
The links are not to help you find the answer to your question, the links are intended to help you improve your question so that we can help you answer your question. When I ask the question "what does your data look like", I don't want to know it has m rows and n columns ... I want to see actual data, in some representative form. If your data is somewhat large, then (as I suggested before and is mentioned in two of the links) use dput(head(x)). - r2evans

2 Answers

0
votes

it can be done with purrr package

try with below :

library(purrr) df<-df %>% map_if(is.factor,as.character) %>% as.matrix

0
votes

(I named it m so that I don't override the matrix function.)

First, your first column is an identifier. I'm going to infer that they have meaning, so I'll keep them around as row-names, but that doesn't change the outcome.

head(m)
#      Provider.State 039          057          064           065         
# [1,] "AK"           " 156023.01" NA           "  279984.88" "  277346.4"
# [2,] "AL"           " 934292.20" " 647281.97" " 1785117.72" " 3231424.9"
# [3,] "AR"           " 565543.16" " 243467.03" " 1210217.08" " 1784411.7"
# [4,] "AZ"           " 859246.77" " 222016.05" " 1738388.11" " 2539940.3"
# [5,] "CA"           "1802826.03" "1955376.54" "12313826.52" "13107647.6"
# [6,] "CO"           " 236048.04" " 284157.80" " 1033786.31" " 1623508.4"

rn <- m[,1]
m <- m[,-1]
rn
#  [1] "AK" "AL" "AR" "AZ" "CA" "CO" "CT" "DC" "DE" "FL" "GA" "HI" "IA" "ID" "IL" "IN" "KS" "KY" "LA" "MA"
head(m)
#      039          057          064           065         
# [1,] " 156023.01" NA           "  279984.88" "  277346.4"
# [2,] " 934292.20" " 647281.97" " 1785117.72" " 3231424.9"
# [3,] " 565543.16" " 243467.03" " 1210217.08" " 1784411.7"
# [4,] " 859246.77" " 222016.05" " 1738388.11" " 2539940.3"
# [5,] "1802826.03" "1955376.54" "12313826.52" "13107647.6"
# [6,] " 236048.04" " 284157.80" " 1033786.31" " 1623508.4"

(We'll use rn in a minute.) Now we need to convert everything to numbers.

m <- apply(m, 2, as.numeric)
rownames(m) <- rn
head(m)
#         039       057        064        065 
# AK  156023.0        NA   279984.9   277346.4
# AL  934292.2  647282.0  1785117.7  3231424.9
# AR  565543.2  243467.0  1210217.1  1784411.7
# AZ  859246.8  222016.0  1738388.1  2539940.3
# CA 1802826.0 1955376.5 12313826.5 13107647.6
# CO  236048.0  284157.8  1033786.3  1623508.4

And now the heatmap works.

heatmap(m)

heatmap plot