matrix2xyz: Matrix Format Conversion

View source: R/matrix2xyz.R

matrix2xyzR Documentation

Matrix Format Conversion

Description

These are functions to convert between different matrix formats. Generally, we may convert a two-dimensional array of values to a linear xyz format which contains the coordinates or indices of the rows and columns corresponding to each matrix element z.

Usage

matrix2xyz(m, na.rm = FALSE)

xyz2matrix(x, y, z, fun)

Arguments

m

Matrix.

na.rm

Logical value specifying whether to remove NA values in z the output. The default is FALSE.

x

Vector containing matrix row indices. Alternatively, x may be a three-column matrix or data frame containing columns specifying x, y and z.

y

Vector containing the matrix column indices. Must be the same length as x.

z

Vector containing the matrix elements which are mapped by x and y. Must be the same length as x and y.

fun

Function (e.g. mean) to be used on z when the indices x and y have duplicate entries.

Value

Matrix

Functions

  • matrix2xyz(): Convert from matrix to xyz format.

  • xyz2matrix(): Convert from xyz to matrix format.

Examples

# Define  random matrix:
m <- round(matrix(runif(40), nrow = 8), 2)

# Convert to XYZ format:
v <- matrix2xyz(m)

# Convert back to matrix format:
xyz2matrix(x = v[,"x"], y = v[,"y"], z = v[,"z"])

# Equivalently:
xyz2matrix(v)

# Convert XYZ elements with duplicate entries to matrix:
x <- c(1, 1, 3, 2, 3)
y <- c(2, 2, 1, 2, 3)
z <- 1:5
m <- xyz2matrix(x, y, z, mean)

# Convert back to XYZ format:
v <- matrix2xyz(m, na.rm = TRUE)


TobieSurette/gulf.utils documentation built on Dec. 1, 2024, 6:01 a.m.