Learn R Programming

sparsevctrs (version 0.3.3)

sparse-arithmatic-scalar: Scalar arithmatic with sparse vectors

Description

Do Arithmatic on sparse vectors without destroying the sparsity. Note that only multiplication and division preserves the default value.

Usage

sparse_division_scalar(x, val)

sparse_multiplication_scalar(x, val)

sparse_addition_scalar(x, val)

sparse_subtraction_scalar(x, val)

Value

A sparse vector of same type.

Arguments

x

A sparse vector.

val

A single numeric value.

Details

No checking of the inputs are being done.

sparse_division_scalar() and sparse_multiplication_scalar() are the most used ones, as they preserve the default, which is often what you want to do.

sparse_division_scalar() always produces double vectors, regardless of whether they could be represented as integers or not. Expect when val = 1 as the input is returned unchanged, or val = NA as the input returned will be NA or the appropiate type.

Examples

Run this code
x_sparse <- sparse_double(c(pi, 5, 0.1), c(2, 5, 10), 10)

sparse_division_scalar(x_sparse, 2)
sparse_multiplication_scalar(x_sparse, 2)
sparse_addition_scalar(x_sparse, 2)
sparse_subtraction_scalar(x_sparse, 2)

Run the code above in your browser using DataLab