Skip to contents

Compute quantiles

Usage

fast_quantile(x, prob = 0.5, na.rm = FALSE, ...)

fast_median(x, na.rm = FALSE, ...)

fast_mvquantile(x, prob = 0.5, na.rm = FALSE, ...)

fast_mvmedian(x, na.rm = FALSE, ...)

Arguments

x

numerical-value vector for fast_quantile and fast_median, and column-major matrix for fast_mvquantile and fast_mvmedian

prob

a probability with value from 0 to 1

na.rm

logical; if true, any NA are removed from x before the quantiles are computed

...

reserved for future use

Value

fast_quantile and fast_median calculate univariate quantiles (single-value return); fast_mvquantile and fast_mvmedian

calculate multivariate quantiles (for each column, result lengths equal to the number of columns).

Examples


fast_quantile(runif(1000), 0.1)
#> [1] 0.1095538
fast_median(1:100)
#> [1] 50.5

x <- matrix(rnorm(100), ncol = 2)
fast_mvquantile(x, 0.2)
#> [1] -1.0772268 -0.6432703
fast_mvmedian(x)
#> [1] 0.1461609 0.2094427

# Compare speed for vectors (usually 30% faster)
x <- rnorm(10000)
microbenchmark::microbenchmark(
  fast_median = fast_median(x),
  base_median = median(x),
  # bioc_median = Biobase::rowMedians(matrix(x, nrow = 1)),
  times = 100, unit = "milliseconds"
)
#> Unit: milliseconds
#>         expr      min        lq      mean    median        uq      max neval
#>  fast_median 0.090669 0.1297725 0.1412165 0.1412690 0.1567375 0.174656   100
#>  base_median 0.201425 0.2115350 0.2180421 0.2166895 0.2220045 0.321030   100

# Multivariate cases
# (5~7x faster than base R)
# (3~5x faster than Biobase rowMedians)
x <- matrix(rnorm(100000), ncol = 20)
microbenchmark::microbenchmark(
  fast_median = fast_mvmedian(x),
  base_median = apply(x, 2, median),
  # bioc_median = Biobase::rowMedians(t(x)),
  times = 10, unit = "milliseconds"
)
#> Unit: milliseconds
#>         expr      min       lq      mean    median       uq      max neval
#>  fast_median 0.656075 0.712320 0.7571688 0.7402175 0.812898 0.868451    10
#>  base_median 2.660367 2.719668 2.7893278 2.7841630 2.818021 3.022844    10