[R] Matrix entries not recognised as numeric

Paul Smith phhs80 at gmail.com
Mon Sep 17 15:29:46 CEST 2007


On 9/17/07, snapperball <kingsley.oteng at gmail.com> wrote:
> 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)
>
> 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?

Use as.numeric:

> m <- matrix(c("1","2","3","4"),2,2)
> m
     [,1] [,2]
[1,] "1"  "3"
[2,] "2"  "4"
> range(as.numeric(m[,1]))
[1] 1 2

Paul



More information about the R-help mailing list