Aggregate values in x for bins defined on y: all values
in x for values in y falling into a bin (defined on y) are
aggregated with the provided function FUN.
Arguments
- x
numericwith the values that should be aggregated/binned.- y
numericwith same length thanxwith values to be used for the binning.ymust be increasingly sorted, or else an error will be thrown.- size
numeric(1)with the size of a bin.- breaks
numericdefining the breaks (bins). Seebreaks_ppm()to define breaks with increasing size (depending on ppm).- FUN
functionto be used to aggregate values ofxfalling into the bins defined bybreaks.FUNis expected to return anumeric(1).- returnMids
logical(1)whether the midpoints for the breaks should be returned in addition to the binned (aggregated) values ofx. SettingreturnMids = FALSEmight be useful if the breaks are defined before hand and binning needs to be performed on a large set of values (i.e. within a loop for multiple pairs ofxandyvalues).- .check
logical(1)whether to check thatyis an ordered vector. Setting.check = FALSEwill improve performance, provided you are sure thatyis always ordered.
Value
Depending on the value of returnMids:
returnMids = TRUE(the default): returns alistwith elementsx(aggregated values ofx) andmids(the bin mid points).returnMids = FALSE: returns anumericwith just the binned values forx.
Examples
## Define example intensities and m/z values
ints <- abs(rnorm(20, mean = 40))
mz <- seq(1:length(ints)) + rnorm(length(ints), sd = 0.001)
## Bin intensities by m/z bins with a bin size of 2
bin(ints, mz, size = 2)
#> $x
#> [1] 40.25532 39.99443 41.14841 39.75580 39.71729 40.62898 42.06502 40.51243
#> [9] 40.54300 39.08593
#>
#> $mids
#> [1] 2 4 6 8 10 12 14 16 18 20
#>
## Repeat but summing up intensities instead of taking the max
bin(ints, mz, size = 2, FUN = sum)
#> $x
#> [1] 116.41801 39.99443 119.94815 79.50848 39.71729 80.07528 80.43404
#> [8] 118.12740 80.49039 39.08593
#>
#> $mids
#> [1] 2 4 6 8 10 12 14 16 18 20
#>
## Get only the binned values without the bin mid points.
bin(ints, mz, size = 2, returnMids = FALSE)
#> [1] 40.25532 39.99443 41.14841 39.75580 39.71729 40.62898 42.06502 40.51243
#> [9] 40.54300 39.08593
