Convert character matrix into numeric matrix
There isn't really a problem here at all, not with most options I tried. You are getting Warnings but these pertain to the "NA"
strings, which because they aren't NA
nor a number stored in a string, R doesn't know what to do with them and changes these to NA
. This is all the warning is telling you. Hence
apply(extra4, 2, as.numeric)
sapply(extra4, as.numeric)
class(extra4) <- "numeric"
storage.mode(extra4) <- "numeric"
all work and all warn about the " NA " values (or variants thereof) in column 22 of extra4
:
Warning message:
In storage.mode(m) <- "numeric" : NAs introduced by coercion
but these are just warnings and in this case can be ignored. If they trouble you, you could wrap the call in suppressWarnings()
> suppressWarnings(storage.mode(m) <- "numeric")
but that is dangerous as it will stop all warnings, not just the one about the NA
s.
Convert character matrix column to numeric matrix
(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)
convert matrix to numeric data frame
Nicest way to convert matrices is to change the mode
. This way you can make the matrix numeric
, then you can convert to data frame easily:
mode(datamatrix) = "numeric"
data.frame(datamatrix)
# X1 X2 X3 X4 X5 X6 X7
# 1 1 0.9301 0.9000 0.8042 0.7487 0.6951 0.6889
# 2 2 0.9300 0.8064 0.7847 0.7105 0.6770 0.6471
# 3 3 0.9286 0.7947 0.7832 0.6566 0.6588 0.6524
# 4 4 0.9209 0.7607 0.7578 0.5951 0.5922 0.5932
How to convert a character matrix to numeric without affecting row/col-names
The problem may be that the decimal is a comma instead of a period. Try first converting that.
dataset_numeric <- sub(",",".",dataset)
Once that is done, this should be fairly simple. If starting from here, this may be a duplicate of the following with the added requirement of row names.
Convert character matrix into numeric matrix
So in this case you can modify slightly:
dataset_numeric <- apply(dataset_numeric, 2, as.numeric)
rownames(dataset_numeric) <- rownames(dataset)
Or choose this option:
class(dataset_numeric) <- "numeric"
To test:
prcomp(dataset_numeric, center = TRUE, scale = TRUE)
Runs without an error:
Standard deviations (1, .., p=6):
[1] 2.191373e+00 1.464462e+00 1.331818e+00 1.002092e+00 5.246949e-01 3.755055e-15
Rotation (n x k) = (10 x 6):
PC1 PC2 PC3 PC4 PC5 PC6
ACVR1 -0.33509491 -0.32378624 0.35207791 0.04650037 -0.22465986 -0.07403592
ADAM17 -0.26169241 -0.47259488 -0.30394898 -0.13763357 -0.18328981 0.41562880
AGER -0.07354562 0.38073508 -0.56645061 0.26681868 -0.28597500 0.12602119
AKT1 -0.37111066 0.01674254 -0.07923664 -0.48941844 0.56009962 0.31877982
ANPEP -0.41234145 0.25398752 -0.06276181 0.12397346 -0.28744359 0.12200886
ANXA1 -0.34908735 -0.20718967 0.18610579 0.51004989 -0.01539492 0.28629143
AR -0.23808868 0.54584757 0.08481153 -0.27218135 -0.07711181 0.16714943
ATM 0.37104240 -0.14079095 0.04995052 -0.44945864 -0.56884559 0.33723134
AURKA -0.20262305 -0.29758992 -0.57407802 -0.16727601 -0.03025329 -0.51762461
AXIN1 -0.38573848 0.11317416 0.28050560 -0.29761514 -0.32731009 -0.44477115
How to convert character matrix to numeric, keeping first column as row name: R
We can do
y <- `dimnames<-`(`dim<-`(as.numeric(y), dim(y)), dimnames(y))
y
# treatmenta treatmentb
#John Smith NA 2
#John Doe 16 11
#Mary Johnson 3 1
Or a compact option is
class(y) <- "numeric"
data
y <- structure(c(NA, "16", " 3", " 2", "11", " 1"), .Dim = c(3L, 2L
), .Dimnames = list(c("John Smith", "John Doe", "Mary Johnson"
), c("treatmenta", "treatmentb")))
convert elements of matrix into numeric from character
You can try, if m
is your matrix:
`class<-`(m, 'numeric')
Why won't my matrix convert from character to numeric?
Here is one way to convert a character
matrix to a numeric
matrix:
m = matrix(as.character(1:9), 3, 3)
m
## [,1] [,2] [,3]
## [1,] "1" "4" "7"
## [2,] "2" "5" "8"
## [3,] "3" "6" "9"
apply(m, 2, as.numeric)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Related Topics
How to Define More Line Types for Graphs in R (Custom Linetype)
How to Delete Groups Containing Less Than 3 Rows of Data in R
Using Get() with Replacement Functions
Split One Row into Multiple Rows
Change Values in Multiple Columns of a Dataframe Using a Lookup Table
Calculate Group Mean While Excluding Current Observation Using Dplyr
List for Multiple Plots from Loop (Ggplot2) - List Elements Being Overwritten
Divide Row Value by Aggregated Sum in R Data.Frame
Dt: Dynamically Change Column Values Based on Selectinput from Another Column in R Shiny App
Reading Multiple Files into Multiple Data Frames
How to Color Sliderbar (Sliderinput)
Shift Values in Single Column of Dataframe Up
Reverse Datetime (Posixct Data) Axis in Ggplot
Function to Split a Matrix into Sub-Matrices in R
Rstudio Shiny Error: There Is No Package Called "Shinydashboard"
Connect to Postgres via Ssl Using R
How to Filter Rows Based on Difference in Dates Between Rows in R
R: What Do You Call the :: and ::: Operators and How Do They Differ