Skip to contents

Computes the median along an axis. Equivalent to nv_quantile(x, 0.5, axis, 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(x, axis = NULL, interpolation = "linear", nan_rm = FALSE)

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

Arguments

x

(arrayish)
Input array.

axis

(integer(1) | NULL)
Axis along which to compute the median. Negative values count from the end, i.e. -1 refers to the last axis. If NULL (default), uses the last axis.

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.

na.rm

Forwarded to nv_median()'s nan_rm argument.

...

No additional arguments.

Value

arrayish
Same shape as x with axis 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),
  axis = 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{} ]