HenrikBengtsson / matrixStats

R package: Methods that Apply to Rows and Columns of Matrices (and to Vectors)
https://cran.r-project.org/package=matrixStats
203 stars 33 forks source link

colMeans2 does row wise calculation with newer R versions #247

Open itsme24 opened 7 months ago

itsme24 commented 7 months ago

Dear all, I get a weird behaviour with R version 4.3.1 which does not calculate the colMeans2 any more but does a row-wise calculation. This is not the case with e.g. R 4.2.2.

I couldn't figure out if and where the feature in the c-code this is coming from. I haven't found another colFunc where I found this row-wise result.

print(R.version.string)

a<- data.table(rebalancing=rep(c(1L,NA), 5) , coverage=rep(c(1.0,0.5,0.9,0.5,0.8),2), b='b')

print('base')
print(a[, colMeans(cbind(coverage*rebalancing), na.rm=T) , by='b'] )

print('matrixStats')
a[, matrixStats::colMeans2(cbind(coverage*rebalancing), na.rm=T) , by='b']

image

yaccos commented 7 months ago

I have tried out your example with R 4.3.2 and matrixStats 1.2.0. When running it there is no difference between the output using base::colMeans() and matrixStats::colMeans2(). For this particular example, it would be more convenient to writea[, mean(coverage*rebalancing, na.rm=T), by='b'], eliminating the need for creating a one-column matrix.