Thaden, John J
2006-Sep-04 21:16 UTC
[R] Coercing elements of a matrix from integer to double
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}}
Peter Dalgaard
2006-Sep-04 21:28 UTC
[R] Coercing elements of a matrix from integer to double
"Thaden, John J" <ThadenJohnJ at uams.edu> writes:> 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?storage.mode(M) <- "double" -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907