It didn't work for me until I loaded the zoo
package. Before that it was because of coercion issues between dates and numeric and not supplying an origin
. If you try detach(package:zoo)
and run:
a <- as.Date("2012-10-01"):as.Date("2013-10-01")
myFun <- function (x) {
return(sqrt(abs( x - as.numeric( as.Date( "2013-01-01" ) ) ) ) )
}
sapply(a,myFun)
I get back the same thing that I do when I load package zoo
. The reason is that (on my R session) without zoo
running, the first command results in:
as.Date(as.Date("2012-10-01"):as.Date("2013-03-25"))
Results in:
Error in as.Date.numeric(as.Date("2012-10-01"):as.Date("2013-03-25")) :
'origin' must be supplied