I’m using R’s .Call interface to C to pass and return real-valued vectors (REALSXP) which may include NA values. For vectors being passed into C, I need to detect the existence of NA values, and in vectors which I return from C I need to assign the NA value.
Suppose vec is a SEXP object that has been set equal to a vector containing REALSXP objects. For instance, if allocating this in C, the code might be
SEXP vec;
PROTECT(vec = allocVector(REALSXP, 10));
With respect to detecting an NA value, I know the following will work:
isnan(vec[index])
where isnan() is defined in math.h. Is this the preferred way for detecting NA values?
I do not know how to assign NA values. I know that I can do something like the following,
vec[index] = 0.0/0.0;
to assign an NaN. I’m not sure exactly what the difference is between NaN and NA in R, but I would prefer to assign NA instead of NaN.
EDIT
Found the answer. Functions ISNA() and ISNAN() are defined in R_ext/Arith.h and so is the symbol R_NaReal which can be used to assign an NA value.
Found the answer to my question. Functions
ISNA()andISNAN()are defined in R_ext/Arith.h and so is the symbolR_NaRealwhich can be used to assign an NA value.