sparse_var: Calculate variance from sparse vectors

View source: R/sparse_var.R

sparse_varR Documentation

Calculate variance from sparse vectors

Description

Calculate variance from sparse vectors

Usage

sparse_var(x, na_rm = FALSE)

Arguments

x

A sparse numeric vector.

na_rm

Logical, whether to remove missing values. Defaults to FALSE.

Details

This function, as with any of the other helper functions assumes that the input x is a numeric vector. This is done for performance reasons, and it is thus the users responsibility to perform input checking. Non-sparse numeric vectors are supported but get no performance benefit.

Like var(), it returns NA when fewer than 2 non-missing observations remain.

Much like var() it uses the denominator n-1.

Value

single numeric value.

Examples

sparse_var(
  sparse_double(1000, 1, 1000)
)

sparse_var(
  sparse_double(1000, 1, 1000, default = 1)
)

sparse_var(
  sparse_double(c(10, 50, 11), c(1, 50, 111), 1000)
)

sparse_var(
  sparse_double(c(10, NA, 11), c(1, 50, 111), 1000)
)

sparse_var(
  sparse_double(c(10, NA, 11), c(1, 50, 111), 1000),
  na_rm = TRUE
)


sparsevctrs documentation built on Nov. 2, 2026, 1:09 a.m.