Skip to contents

Computes the variance along the specified axes.

Usage

nv_var(x, axes = NULL, drop = TRUE, correction = 1L, nan_rm = FALSE)

Arguments

x

(arrayish)
One input. Can be any data type. An R value materializes at its default data type.

axes

(integer() | NULL)
Axes to reduce over. Negative values count from the end, i.e. -1 refers to the last axis. If NULL (default), reduces over all axes.

drop

(logical(1))
Whether to drop the reduced axes: removed from the output shape if TRUE, set to 1 if FALSE.

correction

(integer(1))
Degrees of freedom correction. Default is 1 (Bessel's correction).

nan_rm

(logical(1))
How to handle NaN values in float inputs. If FALSE (default), NaN propagates. If TRUE, NaN values are skipped.

Value

(arrayish)
Has the input's data type where that is a float, and the default float data type (see default_dtypes()) otherwise. The shape is the input's with the reduced axes removed (drop = TRUE) or set to 1 (drop = FALSE).

Details

Uses Bessel's correction by default (correction = 1), matching R's var(). Set correction = 0 for population variance.

See also

Examples

x <- nv_array(1:5)
# the result is a float, even though the input is an integer
nv_var(x)
#> AnvlArray
#>  2.5000
#> [ CPUf32{} ] 

# Bessel's correction by default, correction = 0 for the population variance
nv_var(x, correction = 0L)
#> AnvlArray
#>  2
#> [ CPUf32{} ] 

# NaN propagates unless nan_rm = TRUE
nv_var(nv_array(c(1, NaN, 3, 5)))
#> AnvlArray
#>  nan
#> [ CPUf32{} ] 
nv_var(nv_array(c(1, NaN, 3, 5)), nan_rm = TRUE)
#> AnvlArray
#>  4
#> [ CPUf32{} ]