Hi- I have a 37 X 473971 character matrix that I am trying to convert into a numeric matrix. When I use the code: class(matrix) = "numeric" I end up with something called a "double matrix" whose dimensions are still 37 X 473971 I have also tried new = apply(matrix,2, as.numeric) and got the same thing. The analysis code I am ultimately attempting to run on this data requires that it be in a numerical matrix, and it is really not okay with a double matrix. Does anyone know how to fix this? Thanks. -- Jessica R.B. Musselman, MS T32 Trainee/Doctoral Candidate University of Minnesota Department of Pediatrics Division of Epidemiology/Clinical Research Mayo Mail Code 715 Room 1-195 Moos Tower 420 Delaware St. SE Minneapolis MN 55455 Phone: (612)626-3281 email: bruce087 at umn.edu
What are the entries in your matrix? If they are something that won't coerce to numeric, you need to backtrack. Note how R distinguishes types of characters.> as.numeric("a")[1] NA Warning message: NAs introduced by coercion> as.character(2)[1] "2"> as.numeric("2")[1] 2 On Jul 31, 2013, at 1:47 PM, bruce087 at umn.edu wrote:> Hi- > > I have a 37 X 473971 character matrix that I am trying to convert into a numeric matrix. When I use the code: > > class(matrix) = "numeric" > I end up with something called a "double matrix" whose dimensions are still 37 X 473971 > > I have also tried > new = apply(matrix,2, as.numeric) and got the same thing. > > The analysis code I am ultimately attempting to run on this data requires that it be in a numerical matrix, and it is really not okay with a double matrix. > > Does anyone know how to fix this? > > Thanks. > > -- > Jessica R.B. Musselman, MS > T32 Trainee/Doctoral Candidate > University of Minnesota > Department of Pediatrics > Division of Epidemiology/Clinical Research > Mayo Mail Code 715 > Room 1-195 Moos Tower > 420 Delaware St. SE > Minneapolis MN 55455 > Phone: (612)626-3281 > email: bruce087 at umn.edu > > ______________________________________________ > R-help at 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.Don McKenzie, Research Ecologist Pacific WIldland Fire Sciences Lab US Forest Service Affiliate Professor School of Forest Resources, College of the Environment CSES Climate Impacts Group University of Washington phone: 206-732-7824 dmck at uw.edu
In R, "double" is a synonym for "numeric". Please see ?numeric The details section of ?numeric begins with Details: 'numeric' is identical to 'double' (and 'real'). It creates a double-precision vector of the specified length with each element equal to '0'. Rich On Wed, Jul 31, 2013 at 4:47 PM, <bruce087@umn.edu> wrote:> Hi- > > I have a 37 X 473971 character matrix that I am trying to convert into a > numeric matrix. When I use the code: > > class(matrix) = "numeric" > I end up with something called a "double matrix" whose dimensions are > still 37 X 473971 > > I have also tried > new = apply(matrix,2, as.numeric) and got the same thing. > > The analysis code I am ultimately attempting to run on this data requires > that it be in a numerical matrix, and it is really not okay with a double > matrix. > > Does anyone know how to fix this? > > Thanks. > > -- > Jessica R.B. Musselman, MS > T32 Trainee/Doctoral Candidate > University of Minnesota > Department of Pediatrics > Division of Epidemiology/Clinical Research > Mayo Mail Code 715 > Room 1-195 Moos Tower > 420 Delaware St. SE > Minneapolis MN 55455 > Phone: (612)626-3281 > email: bruce087@umn.edu > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hello, "double" and "numeric" are the same. From the help page for ?double, section "Note on names" "It is a historical anomaly that R has two names for its floating-point vectors, double and numeric (and formerly had real)." Apparently you are successfully converting characters to double precision floating-point numbers. Hope this helps, Rui Barradas Em 31-07-2013 21:47, bruce087 at umn.edu escreveu:> Hi- > > I have a 37 X 473971 character matrix that I am trying to convert into a > numeric matrix. When I use the code: > > class(matrix) = "numeric" > I end up with something called a "double matrix" whose dimensions are > still 37 X 473971 > > I have also tried > new = apply(matrix,2, as.numeric) and got the same thing. > > The analysis code I am ultimately attempting to run on this data > requires that it be in a numerical matrix, and it is really not okay > with a double matrix. > > Does anyone know how to fix this? > > Thanks. >
It is hard to understand that your R code will not work with a double matrix since double is just short for double precision floating point matrix. Your only alternative would be integer.>From ?numeric"It is a historical anomaly that R has two names for its floating-point vectors, double and numeric (and formerly had real). "double is the name of the type. numeric is the name of the mode and also of the implicit class." ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of bruce087 at umn.edu Sent: Wednesday, July 31, 2013 3:48 PM To: r-help at r-project.org Subject: [R] double matrix? Hi- I have a 37 X 473971 character matrix that I am trying to convert into a numeric matrix. When I use the code: class(matrix) = "numeric" I end up with something called a "double matrix" whose dimensions are still 37 X 473971 I have also tried new = apply(matrix,2, as.numeric) and got the same thing. The analysis code I am ultimately attempting to run on this data requires that it be in a numerical matrix, and it is really not okay with a double matrix. Does anyone know how to fix this? Thanks. -- Jessica R.B. Musselman, MS T32 Trainee/Doctoral Candidate University of Minnesota Department of Pediatrics Division of Epidemiology/Clinical Research Mayo Mail Code 715 Room 1-195 Moos Tower 420 Delaware St. SE Minneapolis MN 55455 Phone: (612)626-3281 email: bruce087 at umn.edu ______________________________________________ R-help at 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.
In R "double" and "numeric" mean essentially the same thing. I think you are fine. (What called the result a "double matrix"?) > z <- cbind(c("11", "12"), c("3.14", "2.718")) > str(z) chr [1:2, 1:2] "11" "12" "3.14" "2.718" > class(z) [1] "matrix" > > class(z) <- "numeric" > str(z) num [1:2, 1:2] 11 12 3.14 2.72 > class(z) [1] "matrix" > z [,1] [,2] [1,] 11 3.140 [2,] 12 2.718 > log(z) [,1] [,2] [1,] 2.397895 1.1442228 [2,] 2.484907 0.9998963 R "numeric" vectors consist of C "double" or Fortran "double precision" or "real*8" values - 8 byte double precision floating point numbers with 52 binary digits of precision. S supported 4-byte single precision vectors which it also considered "numeric". Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of bruce087 at umn.edu > Sent: Wednesday, July 31, 2013 1:48 PM > To: r-help at r-project.org > Subject: [R] double matrix? > > Hi- > > I have a 37 X 473971 character matrix that I am trying to convert into a > numeric matrix. When I use the code: > > class(matrix) = "numeric" > > I end up with something called a "double matrix" whose dimensions are still > 37 X 473971 > > I have also tried > > new = apply(matrix,2, as.numeric) and got the same thing. > > The analysis code I am ultimately attempting to run on this data requires > that it be in a numerical matrix, and it is really not okay with a double > matrix. > > Does anyone know how to fix this? > > Thanks. > > -- > Jessica R.B. Musselman, MS > T32 Trainee/Doctoral Candidate > University of Minnesota > Department of Pediatrics > Division of Epidemiology/Clinical Research > Mayo Mail Code 715 > Room 1-195 Moos Tower > 420 Delaware St. SE > Minneapolis MN 55455 > Phone: (612)626-3281 > email: bruce087 at umn.edu > > ______________________________________________ > R-help at 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.