Bug 15655 - approx 'constant' with Inf values gives incorrect results
approx 'constant' with Inf values gives incorrect results
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Language
R 3.0.2
x86_64/x64/amd64 (64-bit) OS X Mavericks
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2014-02-04 18:42 UTC by Robert Almgren
Modified: 2014-02-05 17:16 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Almgren 2014-02-04 18:42:10 UTC
> t1 <- c(1,3,5)
> x1 <- c(2,1,Inf)
> tt <- c(0,2,4,6)
> approx( t1, x1, tt, method='constant', f=0 )
$x
[1] 0 2 4 6

$y
[1]  NA   2 NaN  NA


The 3rd value, at tt=4, should be 1 from t1=3, x1=1, not NaN.

The 4th value, at tt=6, should be Inf from t1=5, x1=Inf, not NA.
Comment 1 Duncan Murdoch 2014-02-04 19:12:01 UTC
Your suggested output might be more desirable than the current output, but it isn't a bug, it is behaving as documented.  The value is documented to be
output as y0*(1-f)+y1*f, where y0 is the value to the left and y1 is the value to the right.  

For tt=4, we have y0 = 1 and y1 = Inf, and the output should be 1*1 + Inf*0.  This is NaN.  

For tt=6, we have y0 = Inf and y1 = NA, so the result is NA. (You are asking for extrapolation here; you'd get NA even if the values were finite.)

An argument could be made to special-case f=0 and f=1 to return y0 and y1 respectively.  If nobody argues against that, I'll look into it.
Comment 2 Duncan Murdoch 2014-02-05 17:16:59 UTC
Fixed in R-devel and R-patched.