Browse code

Fix bug in colWeightedMads for zero row input

const-ae authored on 28/09/2020 09:07:03
Showing 2 changed files

... ...
@@ -361,7 +361,11 @@ setMethod("colWeightedMads", signature(x = "dgCMatrix"),
361 361
     }
362 362
     setNames(reduce_sparse_matrix_to_num(x, function(values, row_indices, number_of_zeros){
363 363
       if(length(values) == 0){
364
-        0.0
364
+        if(number_of_zeros > 0){
365
+          0
366
+        }else{
367
+          NA_real_
368
+        }
365 369
       }else{
366 370
         new_vec <- c(0, values)
367 371
         zero_weight <- sum(w[-(row_indices + 1)])
... ...
@@ -291,6 +291,13 @@ for(idx in seq_along(matrix_list)){
291 291
 
292 292
 
293 293
   test_that("colWeightedMads works", {
294
+    expect_equal(colWeightedMads(sp_mat), matrixStats::colWeightedMads(mat))
295
+    expect_equal(colWeightedMads(sp_mat, na.rm=TRUE), matrixStats::colWeightedMads(mat, na.rm=TRUE))
296
+
297
+    weights <- rep(1, nrow(sp_mat))
298
+    expect_equal(colWeightedMads(sp_mat, w=weights), matrixStats::colWeightedMads(mat, w=weights))
299
+    expect_equal(colWeightedMads(sp_mat, na.rm=TRUE, w=weights), matrixStats::colWeightedMads(mat, w=weights, na.rm=TRUE))
300
+
294 301
     # Test check for length of w
295 302
     expect_error(colWeightedMeans(sp_mat, w=1:42))
296 303
     expect_error(matrixStats::colWeightedMeans(mat, w=1:42))