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

From: Thaden, John J <ThadenJohnJ_at_uams.edu>
Date: Mon 04 Sep 2006 - 21:16:32 GMT


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}}



R-help@stat.math.ethz.ch 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. Received on Tue Sep 05 07:25:54 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 07 Sep 2006 - 07:51:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.