Skip to content

bin: y argument must be an ordered vector #108

@RogerGinBer

Description

@RogerGinBer

Hi there!
I was looking into the function bin for calculating ion-mobility mobilograms (sneumann/xcms#647) and, while experimenting, it seems that the argument y must be an increasingly ordered vector (otherwise it creates a silent bug). I would suggest adding a check for ordered data and/or documenting this requirement in the docs.

I'm guessing that for its original intended purpose (binning mz values) this was not an issue, as mz values are always ordered in a Spectra object.

Here's a reproducible example of the problem:

 xvec <- 1:20
 yvec <- c(41:50, 1:10)
 res_bad <- MsCoreUtils::bin(x = xvec, y=yvec)  ## Incorrect result
 res_good <- MsCoreUtils::bin(x = xvec[order(yvec)], y=sort(yvec)) ## Correct result
 plot(yvec, xvec)
 plot(res_bad$mids, res_bad$x)
 plot(res_good$mids, res_good$x)

## Vector relative ordering shouldn't matter when binning, yet in here the output$x is reversed
identical(MsCoreUtils::bin(x = 1:10, y=1:10),
               MsCoreUtils::bin(x = rev(1:10), y=rev(1:10))) ## FALSE, but it should be TRUE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions