Skip to content
Prev 34370 / 63421 Next

as.matrix.data.frame( Date ) -> character

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