Skip to contents

Computes the median along a dimension. Equivalent to nv_quantile(operand, 0.5, dim, interpolation); for an even-length axis with the default "linear" interpolation, the average of the two middle values is returned, matching base R's median().

You can also use median() directly on an AnvlArray or AnvlBox; extra arguments (e.g. interpolation) are forwarded via ....

Usage

nv_median(operand, dim = NULL, interpolation = "linear", nan_rm = FALSE)

# S3 method for class 'AnvlArray'
median(x, na.rm = FALSE, ..., dim = NULL, interpolation = "linear")

Arguments

operand

(arrayish)
Operand.

dim

(integer(1) | NULL)
Dimension along which to compute the median. If NULL (default), uses the last dimension.

interpolation

(character(1))
Forwarded to nv_quantile(). One of "linear" (default), "lower", "higher", "nearest", "midpoint".

nan_rm

(logical(1))
Forwarded to nv_quantile(). See its documentation for details.

x

(arrayish)
Same as operand; this is the name used by the base R S3 generic.

na.rm

Forwarded to nv_median()'s nan_rm argument.

...

No additional arguments.

Value

arrayish
Same shape as operand with dim removed.

Examples

nv_median(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)))
#> AnvlArray
#>  3.5000
#> [ CPUf32{} ] 
median(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)))
#> AnvlArray
#>  3.5000
#> [ CPUf32{} ] 
nv_median(nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE),
  dim = 2L
)
#> AnvlArray
#>  3
#>  2
#> [ CPUf32{2} ] 
# forwards through the S3 generic via `...`
median(nv_array(c(1, 2, 3, 4)), interpolation = "lower")
#> AnvlArray
#>  2
#> [ CPUf32{} ] 
nv_median(nv_array(c(1, NaN, 3, 5)))
#> AnvlArray
#>  nan
#> [ CPUf32{} ] 
nv_median(nv_array(c(1, NaN, 3, 5)), nan_rm = TRUE)
#> AnvlArray
#>  3
#> [ CPUf32{} ]