[R] what does "numeric" mean? {was ... as.matrix.data.frame() ...}

Martin Maechler maechler at stat.math.ethz.ch
Mon May 24 22:16:15 CEST 2004


>>>>> "BDR" == Prof Brian Ripley <ripley at stats.ox.ac.uk>
>>>>>     on Mon, 24 May 2004 17:02:21 +0100 (BST) writes:

    BDR> I don't think a POSIXt element *is* numeric (that's a
    BDR> basic atomic vector), 

well, yes, in some strict sense, but at least

 > is.atomic( ISOdatetime(2003,1,1:3,0,0,0))
 [1] TRUE

    BDR> so the new behaviour seems right to me.  The Warning is
    BDR> wrong, though, and will be fixed.

Further note that   data.matrix(.) still does convert to
numeric, and  AFAIK all good books on S / R  tell you to rather
use data.matrix(df) in situations you want a numeric matrix from a
data frame df.

    BDR> On Mon, 24 May 2004, Don MacQueen wrote:

    >> Conversion of a data frame to a matrix using as.matrix() when a 
    >> column of the data frame is POSIXt and all other columns are numeric 
    >> has changed in R 1.9.0 from R 1.8.1. The new behavior issues a 
    >> warning message and converts to a character matrix. In R 1.8.1, such 
    >> an object was converted to a numeric matrix.
    >> 
    >> Here is an example.
    >> 
    >> #### R 1.9.0 ####
    >> > foo <- data.frame( x=1:3,dt=ISOdatetime(2003,1,1:3,0,0,0))
    >> 
    >> > as.matrix(foo)
    >> x   dt         
    >> 1 "1" "2003-01-01"
    >> 2 "2" "2003-01-02"
    >> 3 "3" "2003-01-03"
    >> Warning message:
    >> longer object length is not a multiple of shorter object length in: 
    >> cl == c("Date", "POSIXct", "POSIXlt")


  <................>


    >> From ?as.matrix() in R 1.9.0:
    >> 
    >> 'as.matrix' is a generic function. The method for data frames will
    >> convert any non-numeric/complex column into a character vector
    >> using 'format' and so return a character matrix, except that
    >> all-logical data frames will be coerced to a logical matrix.
    >> 
    >> The POSIXt element is numeric, and so should be converted to numeric
    >> >  is.numeric(foo$dt)
    >> [1] TRUE

that is a point, particularly together with the above
is.atomic(foo$dt)  |-> TRUE

    >> ####
    >> I think this might qualify for bug status, either in and of itself or 
    >> relative to documentation. But I'm not, as the posting guide says, 
    >> "completely and utterly sure". So I'm posting to r-help first...I 
    >> will send a bug report if an R-core member asks me to.
    >> 
    >> Thanks
    >> -Don

Note that with a factor (instead of POSIX*t),
things are similar

 > str(ffoo <- data.frame(x=1:3, f=gl(3,1)))
 `data.frame':	3 obs. of  2 variables:
  $ x: int  1 2 3
  $ f: Factor w/ 3 levels "1","2","3": 1 2 3
 > as.matrix(ffoo)
   x   f  
 1 "1" "1"
 2 "2" "2"
 3 "3" "3"
 > data.matrix(ffoo)
   x f
 1 1 1
 2 2 2
 3 3 3

but in that case,

  > is.numeric(ffoo$f)
  [1] FALSE

hence, all according to the docs

------

Maybe we should either be more specific in  help(as.matrix)
{and more other places ?} about what "numeric" means there,
or consider -- and this may well be unfeasible because it
potentially breaks current code -- to redefine
'is.numeric(.)' to be more restrictive:

e.g.,

   if (is.object(x))  ===>   ! is.numeric(x) 

?

Martin




More information about the R-help mailing list