[R] Coercing elements of a matrix from integer to double

Thaden, John J ThadenJohnJ at uams.edu
Mon Sep 4 23:16:32 CEST 2006


Ive been converting elements of matrices and arrays, e.g.,  from
Integers to double-precision, by vectorizing the matrix and then
remaking it. Alternatively, I can redefine one element as double 
which then redefines them all.  Both methods are quick, so I guess
I shouldn't complain, but I would have thought there'd be something
more obvious. Have I missed it?

Here's my redimensioning example:  

## Matrix M...
M <- 1:2e6 ; dim(Mi) <- c(1e3,2e3)
dim(M)
class(M)
## ...has integer elements, e.g.,
class(M[1,1])

## The as.double() command changes
## these to double-precision, but it
## also strips away dimensions...
Md <- as.double(Mi)
dim(Md)
class(Md)
## ...so I have to put them back.
dim(Md) <- dim(Mi)
dim(Md) 
class(Md)
class(Md[1,1])

Here's my "tail wagging the dog" example:

M[1,1] <- as.double(M[1,1])
class(M[2,2])

Thanks,
-John Thaden

Confidentiality Notice: This e-mail message, including any a...{{dropped}}



More information about the R-help mailing list