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

Dirk Eddelbuettel edd @end|ng |rom deb|@n@org
Sun Aug 24 15:33:58 CEST 2025


In SVN commit r88444, Martin made a change following Mikael's PR #18918. The
one-line synopsis is 'subassignment <complex>[i] <- NA should only touch the
real part' and you can see it all at [1].  Imaginary parts now get a zero.
I am wondering if that cause rowMeans and colMeans to be off?

A quick example under R-devel (as of today):

> x <- matrix(1:9 + 2i, 3)
> x[c(2,4,6,8)] <- NA
> 
> x
     [,1] [,2] [,3]
[1,] 1+2i   NA 7+2i
[2,]   NA 5+2i   NA
[3,] 3+2i   NA 9+2i
> 
> rowMeans(x, TRUE)         # this now differs from R-release
[1] 4+1.333333i 5+0.666667i 6+1.333333i
>

But in R 4.5.1 we get the (here constant) imaginary part as constant just as
we do when we do this 'by hand' as rowSum() appears fine:

> rowSums(x, TRUE)
[1]  8+4i  5+2i 12+4i
> apply(x, 1, \(x) sum(is.finite(x)))   # row count of finite elems 
[1] 2 1 2
> 
> rowSums(x, TRUE) / apply(x, 1, \(x) sum(is.finite(x))) 
[1] 4+2i 5+2i 6+2i
> 

I could be off my rocker here as I don't use complex variables much and am a
little rustic but a rudimentary check suggests my reasoning applies: means of
real and imaginary parts (taken across rows or columns) should be the sum
divided by the number of non-NA elements.  Right now they aren't.

Cheers, Dirk

[1] https://github.com/r-devel/r-svn/commit/d65e9a52318921d1a105421b95e0fa6bebceeb55

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



More information about the R-devel mailing list