Learn R Programming

omnibus (version 1.2.15)

unragMatrix: Turn a "ragged" matrix into a "ragged" vector

Description

This function turns a "ragged" matrix into a vector. Consider a case where you have a matrix that looks like:

1, 0, 1
2, 3, NA
NA, 4, NA

Here, each row represents a series of values, where missing values are represented by NA. This can be turned into a vector form going from left to right and top to bottom of the matrix, as in c(1, 0, 1, 2, 3, 4), plus a vector c(1, 4, 6), which provides the index of the first non-NA value in each row of the matrix in the vector, plus another vector, c(1, 1, 1, 2, 2, 3), indicating the row to which each value in the vector belonged.

Usage

unragMatrix(x, skip = NA)

Value

A list with one vector per matrix, plus 1) a vector named startIndex with indices of start values, and 2) a vector named row with one value per non-skip value in each matrix.

Arguments

x

A matrix.

skip

NA (default), NULL, or a numeric, integer, or character value. Value to not include in the output. If NULL, then no values will be skipped.

Examples

Run this code

# default
x <- matrix(c(1, 0, 1, 2, 3, NA, NA, 4, NA), byrow = TRUE, nrow = 3)
unragMatrix(x)

# skip nothing
unragMatrix(x, skip = NULL)

# skips rows with all "skip" values
y <- matrix(c(1, 0, 1, NA, NA, NA, NA, 4, NA), byrow = TRUE, nrow = 3)
unragMatrix(y)

Run the code above in your browser using DataLab