[R] as.matrix.data.frame() in R 1.9.0 converts to character when it should (?) convert to numeric

Don MacQueen macq at llnl.gov
Mon May 24 17:25:14 CEST 2004


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")

>  version
          _                  
platform sparc-sun-solaris2.8
arch     sparc              
os       solaris2.8         
system   sparc, solaris2.8  
status   Patched            
major    1                  
minor    9.0                
year     2004               
month    04                 
day      30                 
language R                  
>


### R 1.8.1 ####
>  foo <- data.frame( x=1:3,dt=ISOdatetime(2003,1,1:3,0,0,0))
>  foo
   x         dt
1 1 2003-01-01
2 2 2003-01-02
3 3 2003-01-03

>  as.matrix(foo)
   x         dt
1 1 1041408000
2 2 1041494400
3 3 1041580800

>  version
          _                  
platform sparc-sun-solaris2.8
arch     sparc              
os       solaris2.8         
system   sparc, solaris2.8  
status   Patched            
major    1                  
minor    8.1                
year     2003               
month    12                 
day      03                 
language R                  


####
In both versions:
>  class(foo$dt)
[1] "POSIXt"  "POSIXct"

####
In R 1.8.1, as.matrix.data.frame() has these lines:
         if (length(levels(xj)) > 0 || !(is.numeric(xj) || is.complex(xj)) ||
             (!is.null(cl <- attr(xj, "class")) && any(cl == c("POSIXct",
                 "POSIXlt"))))

####
In R 1.9.0 there is instead
         if (length(levels(xj)) > 0 || !(is.numeric(xj) || is.complex(xj)) ||
             (!is.null(cl <- attr(xj, "class")) && any(cl == c("Date",
                 "POSIXct", "POSIXlt"))))

And that, I think, explains the warning message.

####
 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
>

####
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
-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list