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 NAs.


m <- matrix(data = c("1","2","3","4","5","6"), ncol = 2, nrow = 3)

class(m) <- "numeric"

Tags:

Matrix

R