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 2. A waring message is there. I do not want such things to happen. Is there any direct way to convert my original ch. matrix to a numeric matrix ? Thanks -- View this message in context: http://www.nabble.com/A-matrix-problem-tp25472583p25472583.html Sent from the R help mailing list archive at Nabble.com.
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 vectorI'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
On 16-Sep-09 13:40:03, 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 > 2. A waring message is there. > > I do not want such things to happen. Is there any direct way to > convert my original ch. matrix to a numeric matrix ? > Thanksmatn<-as.numeric(mat); dim(matn)<-dim(mat) This does the job (without requiring you to use a specific indication of dimension, e.g. ncol=4), but the warning message will still be there unless, as David Winsemius said, you also use options(warn = -1). However, a warning message such as this does not mean that anything has gone wrong -- it is simply a notification that something which was not the character representation of a number was converted into NA. So you can ignore it. (It would lead to exactly the same result if, instead of "NA" in mat[1,1] and mat[5,3], you had some other string such as "tiger" -- try it! You would still get the warning, and this time there might even be some point to it ... ). Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 16-Sep-09 Time: 15:13:39 ------------------------------ XFMail ------------------------------