[R] coercing NA to complex?

Ross Ihaka ihaka at stat.auckland.ac.nz
Mon May 8 06:22:24 CEST 2000


On Mon, May 08, 2000 at 10:22:11AM +1000, John Peters wrote:
> 
> I struck a problem concatenating two complex vectors with an NA for 
> plotting purposes.  It seems that NA is not automatically coerced to mode 
> complex. In the complex vector "b" below, an NA appears after half the 
> vector, and a zero appears where the NA should be:
> 
> 3+4i 3+4i  NA 3+4i 0+0i 3+4i 3+4i 3+4i 3+4i 285
> 
> This zero seems to coming from free space, as, after running this example 
> a few times, the zero gets replaced by some sort of erratic garbage: 
> 
> 3+4i 3+4i  NA 3+4i 2.466318e+179+6.800902e+199i 3+4i 3+4i 3+4i 3+4i 292 
> 
> Is this a misuse of c() and NA or a bug?  It runs OK in Splus 3.4.
> 
> Observed on R 1.0.1 on both linux (compiled on RedHat 6.2) and win95 
> (rw1001).
> 
> #---------------------------------------------------------------------
> a_rep(3+4i,4)
> 
> for (i in 1:1000){
> 
>    b_c(a,NA,a)                     # b is wrong
>    # b_c(a,as.complex(NA),a)         # b is ok
>   
>    cat(b,i,"\n")
>    x_max(c(1,Mod(b[5])),na.rm=T)
>    if (x>2) stop()
> }
> #---------------------------------------------------------------------
> 
> Thanks for R; this is my first surprise for many months.

Thanks for the bug report!  Access to cut-and-paste makes it all too
easy to avoid thinking about what you are doing.

I am committing a fix to the development tree, but if it is an urgent
problem for you, please replace the "default" case in the function
"ComplexAnswer" in src/main/bind.c with the following:

    default:
        n = LENGTH(x);
        for (i = 0; i < n; i++) {
            xi = INTEGER(x)[i];
            if (xi == NA_INTEGER) {
                COMPLEX(ans_ptr)[ans_length].r = NA_REAL;
                COMPLEX(ans_ptr)[ans_length].i = NA_REAL;
            }
            else {
                COMPLEX(ans_ptr)[ans_length].r = xi;
                COMPLEX(ans_ptr)[ans_length].i = 0.0;
            }
            ans_length++;
        }
        break;

Ross
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list