# R-alpha: bug in approx

Andreas Weingessel Andreas.Weingessel@ci.tuwien.ac.at
Fri, 25 Apr 1997 11:04:21 +0200

```When the function approx is called with the argument rule=3D2, one gets
the error message
Error: NAs in foreign function call (arg 6)
Besides, the meaning of rule=3D1 or rule=3D2 is opposite to that describe=
d
in the help text and used in S-plus.

For example, in R:

R> approx(1:10,2:11,xout=3D5:15,rule=3D1)
\$x
[1]  5  6  7  8  9 10 11 12 13 14 15

\$y
[1]  6  7  8  9 10 11 11 11 11 11 11

R> approx(1:10,2:11,xout=3D5:15,rule=3D2)
Error: NAs in foreign function call (arg 6)

but in S-plus:

> approx(1:10,2:11,xout=3D5:15,rule=3D1)
\$x:
[1]  5  6  7  8  9 10 11 12 13 14 15

\$y:
[1]  6  7  8  9 10 11 NA NA NA NA NA

> approx(1:10,2:11,xout=3D5:15,rule=3D2)
\$x:
[1]  5  6  7  8  9 10 11 12 13 14 15

\$y:
[1]  6  7  8  9 10 11 11 11 11 11 11

The reason for this bug can be found in the last lines of the code of
approx:

if (rule =3D=3D 1) {
low <- y[1]
high <- y[length(x)]
}
else if (rule =3D=3D 2) {
low <- NA
high <- low
}
else stop("invalid extrapolation rule in approx")
y <- .C("approx", as.double(x), as.double(y), length(x),=20
xout =3D as.double(xout), length(xout), as.double(low),=20
as.double(high))\$xout
return(list(x =3D xout, y =3D y))

If (rule =3D=3D 2) the values of low and high are set to NA. Immediately
afterwards, the foreign function "approx" is called with these values,
Error: NAs in foreign function call (arg 6)

To obtain the same behavior as in S-plus (and as in the help-text) the
commands for (rule =3D=3D 1) and (rule =3D=3D 2) have to be exchanged.

Regards,
Andreas

************************************************************************
*                          Andreas Weingessel                          *
************************************************************************
* Institut f=FCr Statistik      *                Tel: (+43 1) 58801 4541 =
*
* Technische Universit=E4t Wien *                Fax: (+43 1)  504 14 98 =
*
* Wiedner Hauptstr. 8-10/1071 *     Andreas.Weingessel@ci.tuwien.ac.at *
* A-1040 Wien, Austria        * http://www.ci.tuwien.ac.at/~weingessel *
************************************************************************

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

```