On Sep 16, 2009, at 9:40 AM, Bogaso wrote:
Hi all,
I have following ch. matrix :
mat
[,1] [,2] [,3] [,4]
[1,] "NA" "0.0671746073115122" "1.15281464953731"
"0.822277316923348"
[2,] "0.113184828073156" "-0.0133150789005112" "0.640912657072027"
"-0.0667317787225847"
[3,] "-1.40593584523871" "1.10755549414758" "0.493828059815449"
"-1.09233516877992"
[4,] "0.577643850085066" "1.10279525071026" "1.16725625310315"
"0.367724768195794"
[5,] "0.746100264271392" "-0.335556133578362" "NA"
"0.328559028366446"
Now I want to convert it to a numeric matrix. So I used following
code :
as.numeric(mat)
[1] NA 0.11318483 -1.40593585 0.57764385 0.74610026
0.06717461
-0.01331508 1.10755549 1.10279525 -0.33555613
[11] 1.15281465 0.64091266 0.49382806 1.16725625 NA
0.82227732
-0.06673178 -1.09233517 0.36772477 0.32855903
Warning message:
NAs introduced by coercion
What I noticed is that :
1. Original matrix converted to vector
I'm not sure if this is a recommended method but it is compact:
> M <- matrix(as.character(c(NA,NA, rnorm(14))), ncol=4)
> M
[,1] [,2] [,3] [,4]
[1,] "NA" "-0.601520920256251" "-1.00727470025995"
"-0.510937067339167"
[2,] NA "-0.643289410284616" "-0.560094940433377"
"1.65539295869595"
[3,] "-1.58527121117001" "-0.303929580683863" "0.656380566243141"
"0.863929178838393"
[4,] "0.466350502132621" "1.85702073433996" "0.349130873645143"
"-0.821650081436582"
I put in both a "real" NA and a character NA just to be sure.
> mode(M) <- "numeric"
Warning message:
In eval(expr, envir, enclos) : NAs introduced by coercion
> M
[,1] [,2] [,3] [,4]
[1,] NA -0.6015 -1.0073 -0.5109
[2,] NA -0.6433 -0.5601 1.6554
[3,] -1.5853 -0.3039 0.6564 0.8639
[4,] 0.4664 1.8570 0.3491 -0.8217
2. A waring message is there.
You can suppress warnings:
options(warn = -1) # and then restore the warning level to the
default of 0
I do not want such things to happen. Is there any direct way to
convert my
original ch. matrix to a numeric matrix ?
Thanks
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
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.