I generally do something like storage.mode(my.matrix) <- "double"
--Todd -- Why do you recite at a play and play at a recital? > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Gavin Simpson > Sent: Monday, June 16, 2008 2:31 PM > To: [EMAIL PROTECTED] > Cc: r-help@r-project.org > Subject: Re: [R] Annoyance in as.numeric > > On Mon, 2008-06-16 at 15:35 -0500, [EMAIL PROTECTED] wrote: > > hi: you can do below but i don't know if it's worth it ? > > > > newx <- data.matrix(data.frame(x)) > > print(newx) > > That doesn't work Mark: > > > str(data.frame(x)) > 'data.frame': 2 obs. of 2 variables: > $ X1: Factor w/ 2 levels "1","2": 1 2 > $ X2: Factor w/ 2 levels "3","4": 1 2 > > data.matrix(data.frame(x)) > X1 X2 > [1,] 1 1 > [2,] 2 2 > > Which is as per documentation - the data.frame() coerces to > factors and > data.matrix represents factor by their internal values. > > One "solution" might be to save the dims and then reapply: > > > mat > [,1] [,2] > [1,] "1" "3" > [2,] "2" "4" > > dims <- dim(mat) > > mat2 <- as.numeric(mat) > > dim(mat) <- dims > > mat2 > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > You could wrap this in a function: > > asMatrix <- function(x, ...) { > dims <- dim(x) > x <- as.double(x) > dim(x) <- dims > return(x) > } > > It would be logical to define a method for as.double for > matrix objects. > However objects of class "matrix" are not objects in the sense of > isObject() and hence method dispatch will not work in this case. > > HTH > > G > > > > > > > > On Mon, Jun 16, 2008 at 4:22 PM, Alberto Monteiro wrote: > > > > > 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 > > > > > > ______________________________________________ > > > R-help@r-project.org mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > > http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > -- > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > Dr. Gavin Simpson [t] +44 (0)20 7679 0522 > ECRC, UCL Geography, [f] +44 (0)20 7679 0565 > Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk > Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ > UK. WC1E 6BT. [w] http://www.freshwaters.org.uk > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.