Computes the probs quantile(s) of an array along a dimension.
probs follows the same scalar-vs-array convention as nv_select()'s
index:
a length-1 numeric (e.g.
0.5) treatsprobsas scalar — the output hasdimremoved, like a reduction;a 1-D R array (e.g.
array(c(0.25, 0.5, 0.75))) prepends a leading dimension of sizelength(probs).
Plain length-K (K > 1) vectors are rejected; wrap with array() to
make the array intent explicit.
Arguments
- operand
(
arrayish)
Operand.- probs
(
numeric(1)| 1-Darray)
One or more probabilities in[0, 1]. Either a length-1 numeric (scalar;dimis dropped) or a 1-Darray(a leading dim of sizelength(probs)is prepended). Plain length-K (K > 1) vectors are rejected — wrap witharray().- dim
(
integer(1)|NULL)
Dimension along which to compute the quantile. IfNULL(default), uses the last dimension.- interpolation
(
character(1))
One of"linear"(default),"lower","higher","nearest","midpoint". See "Interpolation modes".- nan_rm
(
logical(1))
How to handleNaNvalues in floating-point inputs. IfFALSE(default),NaNpropagates. IfTRUE,NaNvalues are skipped.
Value
arrayish
For scalar probs: same shape as x with dim removed. For
array probs: a leading dimension of size length(probs) is
prepended.
Interpolation modes
Let h = (n - 1) * q be the 0-based fractional index for an axis of
length n and probability q, with lo = floor(h), hi = ceil(h),
frac = h - lo. Then:
"linear"(default):(1 - frac) * sorted[lo] + frac * sorted[hi]."lower":sorted[lo]— the lower bracket oflinear."higher":sorted[hi]— the upper bracket oflinear."nearest":sorted[lo]iffrac < 0.5elsesorted[hi]."midpoint":(sorted[lo] + sorted[hi]) / 2.
Examples
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))
nv_quantile(x, 0.5) # = nv_median(x)
#> AnvlArray
#> 3.5000
#> [ CPUf32{} ]
nv_quantile(x, array(c(0.25, 0.5, 0.75)))
#> AnvlArray
#> 1.7500
#> 3.5000
#> 5.2500
#> [ CPUf32{3} ]
nv_quantile(x, 0.5, interpolation = "lower")
#> AnvlArray
#> 3
#> [ CPUf32{} ]
nv_quantile(nv_array(c(1, NaN, 3, 5)), 0.5)
#> AnvlArray
#> nan
#> [ CPUf32{} ]
nv_quantile(nv_array(c(1, NaN, 3, 5)), 0.5, nan_rm = TRUE)
#> AnvlArray
#> 3
#> [ CPUf32{} ]