closure: Closure operation for compositional data

View source: R/utils.R

closureR Documentation

Closure operation for compositional data

Description

Applies the closure operation to a numeric vector, matrix or data frame so that each composition sums to a prescribed constant k.

Usage

closure(X, k = 1)

Arguments

X

A numeric vector, matrix, data frame, or an object coercible to one of these. For matrices and data frames, rows are interpreted as compositions.

k

A numeric vector of length 1 or length nrow(X). Must contain strictly positive values.

Details

If X is:

  • a vector, the returned vector sums to k;

  • a matrix or data frame, closure is applied row-wise, and each row sums to the corresponding value of k.

The argument k may be:

  • a single positive number, recycled to all rows;

  • a numeric vector of length nrow(X), specifying a different closure constant for each row.

For a composition x = (x_1, \dots, x_D) with positive sum, the closure to constant k is

C(x) = k \frac{x}{\sum_{j=1}^D x_j}.

This function requires all entries of X to be finite and non-negative, and every row sum (or the vector sum) must be strictly positive.

Value

If X is a vector, a numeric vector of the same length.

If X is a matrix, a numeric matrix with the same dimensions, dimnames, and row-wise sums equal to k.

If X is a data frame, a data frame with the same row and column names, and row-wise sums equal to k.

Examples

closure(c(2, 3, 5))
closure(c(2, 3, 5), k = 100)

X <- matrix(c(1, 1, 2,
              2, 3, 5), nrow = 2, byrow = TRUE)
closure(X)
closure(X, k = c(1, 100))

df <- data.frame(a = c(1, 2), b = c(1, 3), c = c(2, 5))
closure(df, k = 10)


coda.base documentation built on May 8, 2026, 5:10 p.m.