sweep.HDF5Matrix: Broadcast a vector over an HDF5Matrix (sweep)

View source: R/S3_matvec.R

sweep.HDF5MatrixR Documentation

Broadcast a vector over an HDF5Matrix (sweep)

Description

S3 method of base::sweep() for HDF5Matrix objects. Broadcasts a 1-row HDF5Matrix (acting as the STATS vector) across every row or column of the matrix, element-wise.

Usage

## S3 method for class 'HDF5Matrix'
sweep(
  x,
  MARGIN = 2L,
  STATS,
  FUN = "*",
  check.margin = TRUE,
  paral = NULL,
  threads = NULL,
  compression = NULL,
  ...
)

Arguments

x

An HDF5Matrix (the matrix to sweep over).

MARGIN

Integer. 2 (default, sweep over columns) or 1 (sweep over rows). Corresponds to byrows: MARGIN = 1 means byrows = TRUE.

STATS

An HDF5Matrix with one row or one column (the vector to broadcast). Must be in the same HDF5 file as x.

FUN

Character. Operation: "+", "-", "*" (default), "/", "pow".

check.margin

Ignored (kept for S3 signature compatibility).

paral

Logical or NULL.

threads

Integer or NULL.

compression

Integer (0-9) or NULL.

...

Ignored.

Value

A new HDF5Matrix.

Examples


fn <- tempfile(fileext = ".h5")

mat <- matrix(rnorm(100), 10, 10)
X   <- hdf5_create_matrix(fn, "data/X", data = mat)

# STATS must be an HDF5Matrix with one row or one column
# Create a 1-row vector with column means
col_means_vec <- colMeans(as.matrix(X))
stats_hdf5    <- hdf5_create_matrix(fn, "data/col_means",
                                     data = matrix(col_means_vec, 1, 10))

# Column-center X (MARGIN = 2)
X_c <- sweep(X, 2, stats_hdf5, "-")

# Verify first column is centered
all.equal(as.matrix(X_c)[, 1],
          mat[, 1] - col_means_vec[1])

hdf5_close_all()
unlink(fn)



BigDataStatMeth documentation built on May 15, 2026, 1:07 a.m.