Computes the variance along the specified axes.
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.-1refers to the last axis. IfNULL(default), reduces over all axes.- drop
(
logical(1))
Whether to drop the reduced axes: removed from the output shape ifTRUE, set to 1 ifFALSE.- correction
(
integer(1))
Degrees of freedom correction. Default is1(Bessel's correction).- nan_rm
(
logical(1))
How to handleNaNvalues in float inputs. IfFALSE(default),NaNpropagates. IfTRUE,NaNvalues 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.
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{} ]