[Rd] as.matrix.data.frame( Date ) -> character

Romain Francois romain.francois at dbmail.com
Sat Oct 10 13:53:24 CEST 2009


Hello,

Is this intended:

 > m <- data.frame( date = seq.Date( as.Date("2000-01-01"), 
as.Date("2000-01-05"), by = "day" ) )
 > sapply( m, typeof )
     date
"double"
 > as.matrix( m )
      date
[1,] "2000-01-01"
[2,] "2000-01-02"
[3,] "2000-01-03"
[4,] "2000-01-04"
[5,] "2000-01-05"
 > typeof( as.matrix( m ) )
[1] "character"

In particular :

 > m <- data.frame( date = seq.Date( as.Date("2000-01-01"), by = "day", 
length.out = 50 ), x = rnorm(50) )
 > loess( x ~ date, data= m)
Error: NA/NaN/Inf in foreign function call (arg 2)
In addition: Warning message:
NAs introduced by coercion
 > loess( x ~ as.double(date), data= m)
Call:
loess(formula = x ~ as.double(date), data = m)

Number of Observations: 50
Equivalent Number of Parameters: 4.42
Residual Standard Error: 0.9712


Replacing this line in loess :

x <- as.matrix(x)

by this :

x <- tryCatch( do.call( cbind, lapply( x, as.double ) ), error = 
function(e){
	stop( "predictors must all be numeric" )
} )

does the trick for me.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/BcPw : celebrating R commit #50000
|- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc
`- http://tr.im/yw8E : New R package : sos



More information about the R-devel mailing list