Skip to contents

Slightly faster than quantile with na.rm=TRUE. The internal implementation uses the 'C++' function std::nth_element, which is significantly faster than base R implementation when the length of input x is less than 1e7.

Usage

fastquantile(x, q)

Arguments

x

numerical vector (integers or double)

q

number from 0 to 1

Value

Identical to quantile(x, q, na.rm=TRUE)

Examples


# create input x with NAs
x <- rnorm(10000)
x[sample(10000, 10)] <- NA

# compute median
res <- fastquantile(x, 0.5)
res
#> [1] -0.001544295

# base method
res == quantile(x, 0.5, na.rm = TRUE)
#>  50% 
#> TRUE 
res == median(x, na.rm = TRUE)
#> [1] TRUE

# Comparison
microbenchmark::microbenchmark(
  {
    fastquantile(x, 0.5)
  },{
    quantile(x, 0.5, na.rm = TRUE)
  },{
    median(x, na.rm = TRUE)
  }
)
#> Unit: microseconds
#>                                    expr     min       lq     mean   median
#>            {     fastquantile(x, 0.5) }  88.816 149.8695 162.7785 159.9635
#>  {     quantile(x, 0.5, na.rm = TRUE) } 239.878 263.7325 327.8180 285.5580
#>         {     median(x, na.rm = TRUE) } 167.693 189.8900 252.0953 208.9200
#>        uq     max neval
#>  181.6190 204.682   100
#>  415.0145 563.352   100
#>  328.0480 414.965   100