Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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)

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).

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).

Examples

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

str2str

Convert R Objects from One Structure to Another

v0.1.1
GPL (>= 2)
Authors
David Disabato [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.