RfastOfficial/Rfast

rowMedians - potential issue

andreirozanski opened this issue · 2 comments

Describe the bug
It seems that the wrong value is returned when not using na.RM=TRUE in rowMedians.

To Reproduce

test <- rbind(c(5.61,60.71,0.00,16.55,5.12),c(0,0,0,0,0))
test
     [,1]     [,2]   [,3]   [,4]   [,5]
[1,] 5.61    60.71    0    16.55    5.12
[2,] 0.00     0.00    0     0.00    0.00

Rfast::rowMedians(test)  # problem here
[1] 60.71 0.00

Rfast::rowMedians(test,na.rm=TRUE)  # it works
[1] 5.61 0.00

# also, as one additional check - using colMedians in a transposed test matrix:

Rfast::colMedians(t(tst)) # also works
[1] 5.61 0.00

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • R-Version 4.2.2
  • Rfast-Version 2.0.8

Thanks!

This looks like an off-by-one error here:

https://github.com/RfastOfficial/Rfast/blob/master/inst/include/Rfast/matrix.hpp#L1378

The element after middle will not be sorted. When na.rm=TRUE is set, the med_helper template is used, which does it correctly.

Fixed.