Learn R Programming

str2str (version 1.0.0)

m2lv: Matrix to List of (Atomic) Vectors

Description

m2lv converts a matrix to a list of (atomic) vectors. This is useful since there is no as.list.matrix method. When rownames and/or colnames are NULL, they are replaced by their position numerals so that the dimension information is retained.

Usage

m2lv(m, along, check = TRUE)

Value

list of (atomic) vectors. If along = 1, then the names are the rownames of m and the vectors are rows from m. If along = 2, then the names are the colnames of m and the vector are columns from m. Note, the vectors always have the same length as nrow(m).

Arguments

m

matrix (i.e., array with 2 dimensions).

along

numeric vector of length 1 specifying which dimension to slice the matrix along. If 1, then the matrix is sliced by rows. If 2, then the matrix is sliced by columns.

check

logical vector of length 1 specifying whether to check the structure of the input arguments. For example, check whether m is a matrix. This argument is available to allow flexibility in whether the user values informative error messages (TRUE) vs. computational efficiency (FALSE).

Examples

Run this code
m2lv(VADeaths, along = 1)
m2lv(VADeaths, along = 2)
m2lv(m = as.matrix(x = attitude, rownames.force = TRUE), along = 1)
m2lv(m = as.matrix(x = attitude, rownames.force = TRUE), along = 2)
m2lv(m = as.matrix(x = unname(attitude), rownames.force = FALSE),
   along = 1) # dimnames created as position numerals
m2lv(m = as.matrix(x = unname(attitude), rownames.force = FALSE),
   along = 2) # dimnames created as position numerals
# check = FALSE
try_expr(m2lv(VADeaths, along = 3, check = FALSE)) # less informative error message
try_expr(m2lv(VADeaths, along = 3, check = TRUE)) # more informative error message

Run the code above in your browser using DataLab