0

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 ")))
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 4
    This sounds like you have `factor`s which are being converted to their `integer` indices before being convert to `numeric`. Try `as.numeric(as.character(...))` and see if it changes. – r2evans Oct 16 '18 at 00:05
  • 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 Oct 16 '18 at 22:26
  • 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: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Oct 16 '18 at 22:44
  • 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 Oct 16 '18 at 23:43
  • 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 Oct 16 '18 at 23:50
  • I made added some more detail of my problem. Hope u cud help me in solving tis problem. – Anjan Karpak Oct 17 '18 at 19:34
  • Good start. Please do not post an image of code/data/errors, just the text itself. Several reasons are immediate: I cannot copy code or data from your image into my R console and try it out, and I choose to not transcribe it manually. Some reasons are slightly less obvious but still important, including: it breaks screen readers *hard*; search engines don't read them, so searches will not find it; mobile device screen size might be a limiting factor. Ref: https://meta.stackoverflow.com/a/285557/3358272 – r2evans Oct 17 '18 at 19:36
  • Perhaps this will be enough to help us: **`dput(x[1:20,x:5])`**. Type that into the console, then take the output (starting with `structure(...`) and paste it into a code block in your question. It will help *a lot*. I suspect, however, that your conversion to `matrix` was damaging, in that it converted all of your numbers to `character`. Time will tell. – r2evans Oct 17 '18 at 19:40
  • Added more information as per your wish. Hope tis helps u now – Anjan Karpak Oct 19 '18 at 20:10
  • I think I've addressed your issues in my answer below. In the future, providing that level of information in the original question can make a significant difference in faster and more applicable answers. – r2evans Oct 19 '18 at 20:24
  • Thanks a lot!! It helped me in solving my problem. I would lie to stay connected with u. If u dont mind u can send me an mail at anjankarpak@knights.ucf.edu. Staying connected will help me in learning more in tis domain. – Anjan Karpak Oct 20 '18 at 03:11

2 Answers2

0

it can be done with purrr package

try with below :

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

sai saran
  • 737
  • 9
  • 32
  • As per your command it changes the data frame into list type. But I dont want to change it to any other format as heatmap accepts only matrix format. total<-total %>% map_if(is.factor,as.character) %>% as.matrix – Anjan Karpak Oct 16 '18 at 22:35
0

(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

r2evans
  • 141,215
  • 6
  • 77
  • 149