[R] Matrix entries not recognised as numeric

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Sep 17 15:53:00 CEST 2007


On 17-Sep-07 13:18:10, snapperball wrote:
> 
> Hello, 
> 
> I am using a number of large matrices in a project. I read a
> couple of the matrices using the read.csv command. However
> in some instances, R does not recognize the resulting matrices
> as consisting of numerical values (I suspect R considers them as
> strings or factors)

What is likely, here, is that in those cases which fail the
CSV file has some entries which are not interpretable as numeric
values[1]. In that case, R will treat such entries as "character"
and then do a global conversion to character type.

[1] The exception to that is an entry consisting of "NA", as in

  1.1,1.2,1.3,1.4
  2.1,NA,2.3,2.4
  3.1,3.2,3.3,3.4

when the "NA" entry will become an NA in R, and the rest will
be numbers as they should be.

For example:

matrix(c(1,2,3,4,5,6),nrow=2)
       [,1] [,2] [,3]
  [1,]    1    3    5
  [2,]    2    4    6

matrix(c(1,2,3,"A",5,6),nrow=2)
       [,1] [,2] [,3]
  [1,] "1"  "3"  "5" 
  [2,] "2"  "A"  "6" 

matrix(c(1,2,3,NA,5,6),nrow=2)
       [,1] [,2] [,3]
  [1,]    1    3    5
  [2,]    2   NA    6

I suggest you check the matrix entries somehow (unfortunately
you have "large matrices"). The R command typeof() could be
useful for a first clue, e.g.

typeof(matrix(c(1,2,3,4,5,6),nrow=2))
  [1] "double"

typeof(matrix(c(1,2,3,"A",5,6),nrow=2))
  [1] "character"

typeof(matrix(c(1,2,3,NA,5,6),nrow=2))
  [1] "double"

Another tool would be grep(), for example:

grep("[^0-9.]",matrix(c(1.1,2.1,3.1,"A",5,6),nrow=2))
  [1] 4

which identifies which elements of the 6 elements in the matrix
(counting down columns, i.e. in the order given in the CSV list)
match the regulary expression "[^0-9.]" which means

  "Anything which is not (^) one of 0,1,2,3,4,5,6,7,8,9 (0-9)
   or ."

so that it picks out any element containing anything other than
the characters 0 1 2 3 4 5 6 7 8 9 . which can form part of the
expression of a decimal number (NOTE that this works because
the matrix is of "character" type; if it were of a numeric
type then the "." would not be part of the representation).

Other variants of this kind of search could be considered!

Hoping this helps,
Ted.

> 
> for instance when I try the following command (consider aMatrix is a
> matrix), 
> 
>> range(aMatrix[,1])
> 
> R returns
> 
>> NA
> 
> I get the same output when I try the max  and min commands. Basically R
> does
> not recognise the matrix as consisting of numerals. However when I
> inspect
> the matrix the entries appear to contain numerical values. 
> 
> Is there something I am doing wrong? Is there anyway of coercing R into
> recognizing the matrix as consisting of numerals?
> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Matrix-entries-not-recognised-as-numeric-tf4466621
> .html#a12735603
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 17-Sep-07                                       Time: 14:52:57
------------------------------ XFMail ------------------------------



More information about the R-help mailing list