[Rd] Do rowMeans and colMeans of complex vars need adjusting following r88444?

Dirk Eddelbuettel edd @end|ng |rom deb|@n@org
Mon Aug 25 12:46:56 CEST 2025


On 25 August 2025 at 11:54, Martin Maechler wrote:
| I still agree we should address this:  We do have a discrepancy
| with mean() i.e.,  mean.default()  which does "exactly" what you
| do "by hand" above,  and hence using is.na() for the full
| complex vector, and not *separately* for Re() and Im() parts;
| ... and I do tend to agree that  colMeans(*, na.rm=TRUE) etc
| probably should be adapted to *not* work coordinate-wise but
| drop all "complex NAs" both for Re and Im.

I agree with that.

The culprit is the per-component call (here from colMeans, same for rowMeans)

    z <- if(is.complex(x))
        .Internal(colMeans(Re(x), n, prod(dn), na.rm)) +
            1i * .Internal(colMeans(Im(x), n, prod(dn), na.rm))
    else .Internal(colMeans(x, n, prod(dn), na.rm))

where we may need to add a na.omit() if na.rm is TRUE.

But I didn't like that idea well enough yet yesterday to propose a patch
along those lines as I did not think about the comparison with mean.default
which is quite convincing. Having results differ even within r-devel is quite
bad indeed, and mean.default, swept by hand across rows or columns, also
return what r-release returns here for a matrix containing NAs.

So maybe na.omit() is the best we can do here?

Dirk

-- 
dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org



More information about the R-devel mailing list