0

This is on Rstudio. I understand the usual meaning of "Origin must be supplied" errors and can fix it. But this one is a bit tricky.

Received ~500 lines of R code from somebody who left. I aligned the input CSV according to her spec, verified with her testing CSV file using str(). Everything runs fine, until this line

steps[is.na(step3)] <- 0

The error says " in as.date.numeric(value) " ‘Origin' must be supplied'

Steps now consists of num, int, char, Factor... BUT only one Date, named p-day. I am assuming the error is about this lone Date field? Also: <0 is making R implicitly converting the Date field to numeric before imputing NA to 0? Thanks

user2554330
  • 37,248
  • 4
  • 43
  • 90
opt135
  • 141
  • 1
  • 8
  • Date-classed vectors are numeric mode. And there no "imputing" of NA. The `is.na` function returns a logical value. – IRTFM Mar 12 '18 at 01:34

2 Answers2

0

So the as.Date.numeric function requires an origin. That's R. And it's being implicitly called when you try to assign a zero to a Date-classed vector. The default origin (the Date value of 1) for R and C dates is 1970-01-01. So this probably means the original programmer wanted that value to be the day before the origin. (Or possibly he didn't know very much R...?).

> dt <- as.Date(c("1970-01-01", NA))
> dt
[1] "1970-01-01" NA          
> dt[is.na(dt)] <- 0
Error in as.Date.numeric(value) : 'origin' must be supplied
> mode(dt)
[1] "numeric"
> dt[is.na(dt)] <- dt[1]-1
> dt
[1] "1970-01-01" "1969-12-31"
> dt <- as.Date(c("1970-01-01", NA))
> dt[is.na(dt)] <- dt[1]*0
Error in Ops.Date(dt[1], 0) : * not defined for "Date" objects
> dt[is.na(dt)] <- as.Date("1969-12-31")
> dt
[1] "1970-01-01" "1969-12-31"

You can either change that zero to as.Date(origin) or explicitly assign the day before the default beginning of the epoch.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

I simply took out the date variable, manually set its Origin and re-ran

steps[is.na(step3)] <- 0

Now that the vector does not have the data variable having missing, it works fine

opt135
  • 141
  • 1
  • 8