[R] Annoyance in as.numeric

Alberto Monteiro albmont at centroin.com.br
Mon Jun 16 22:22:07 CEST 2008


Why does as.numeric convert matrices and arrays to vectors?

as.numeric(matrix(c("1", "2", "3", "4"), 2, 2))
[1] 1 2 3 4

I could only figure out ugly ways to bypass this, like:

x <- matrix(c("1", "2", "3", "4"), 2, 2)
array(as.numeric(x), dim = dim(x), dimnames = dimnames(x))

Alberto Monteiro



More information about the R-help mailing list