Repository navigation
Conversation
… (0.99.16) With na.rm = TRUE, rowMeans, colMeans, rowVars, colVars, rowSds and colSds dropped stored NA values from the sum but not from the count. A mean was divided by too many elements, and a variance used the full extent of the margin, so the results disagreed with matrixStats whenever the data held an NA. - Add .nonMissing(), the number of elements of a margin that are not NA: the extent minus the stored rows whose value is NA. The means and variances divide by it, and the variance counts the stored non-NA values with count(y) instead of n(). - A sum with no value left is coalesced to 0, so a margin with nothing left gives 0/0 = NaN for the mean, as base R does. - A variance over fewer than two values is NA. DuckDB returns NaN for 0/0, which made an all-NA margin come back as NaN where matrixStats gives NA. - The inner subtraction is parenthesized explicitly, since dbplyr drops the parentheses of a nested subtraction. Builds on the proposal in #2 (count the non-NA values in the denominator), and adds the guard for margins with too few values that the proposal lacked. Tests compare the six functions with matrixStats on matrices with stored NA values, an all-NA margin, and a margin with a single value, with NA and NaN compared exactly. Both new tests fail on the previous code. Suite: 3486 expectations, 0 failures at 1 and 8 threads.
|
Thank you for this, @buyan-kh. Counting the non-NA values in the I'm closing the PR because the same change is now on main. It differs Issue #1 is closed by the same commit. Please open an issue if you see |
fixes #1
with na.rm = TRUE the means and variances now divide by the number of cells that are not NA, instead of the full row or column length.
added a test that checks rowMeans, colMeans, rowVars, colVars and rowSds against base R and matrixStats on a small matrix with one NA, and that na.rm = FALSE still gives NA. full test suite passes locally.