[R] Missing values in argument of .Fortran.

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jun 6 17:26:41 CEST 2005


On 6/6/2005 9:52 AM, Rolf Turner wrote:
> I wish to pass a vector ``y'', some of whose entries are NAs to a
> fortran subroutine which I am dynamically loading and calling by
> means of .Fortran().  The subroutine runs through the vector entry by
> entry; obviously I want to have it do one thing if y[i] is present
> and a different thing if it is missing.
> 
> The way I am thinking of proceeding is along the xlines of:
> 
> 	ymiss <- is.na(y)
> 	rslt <- .Fortran(
> 		"foo",
> 		NAOK=TRUE,
> 		as.double(y),
> 		as.logical(ymiss),
> 		etc,
> 		etc
> 	)
> 
> and inside ``foo'' have a logical branch based on the value of
> xmiss(i).
> 
> Questions:
> 
> 	(1) Is there a sexier way to proceed?  E.g. is it possible
> 	within (g77) fortran to detect the fact that y(i) is/was an
> 	NA (or not) and make the nature of y(i) the basis of an
> 	if-statement?

In C you can use the macros

ISNA(x) True for R’s NA only
ISNAN(x) True for R’s NA and IEEE NaN
R_FINITE(x) False for Inf, -Inf, NA, NaN

where the R function is.na() is closest to ISNAN(), I think.  There's no 
supplied way to do these things in Fortran, but presumably you could 
call a C function which did one of these tests.

> 	(2) Are there any lurking pitfalls in the use of the NAOK=TRUE
> 	argument?

I think the way you did it looks perfectly safe.  Following my advice 
above will be a little trickier, because some other user of your code 
might use a different Fortran compiler, and it might handle C functions 
differently.

> 	(3) Is there an entirely different and better way to proceed?

I'd do it your way if I was using Fortran.

Duncan Murdoch




More information about the R-help mailing list