View source: R/str2str_functions.R
| m2v | R Documentation |
m2v converts a matrix to a (atomic) vector. The benefit of m2v
over as.vector or c is that 1) the vector can be formed along rows
as well as columns and 2) the dimnames from m can be used for the names of
the returned vector.
m2v(m, along = 2, use.dimnames = TRUE, sep = "_", check = TRUE)
m |
matrix |
along |
numeric vector of length one that is equal to either 1 or 2.
1 means that |
use.dimnames |
logical vector of length 1 that specifies whether the dimnames
of |
sep |
character vector of length 1 specifying the string that will separate
the rownames and colnames in the naming scheme of the return object. Note, |
check |
logical vector of length 1 specifying whether to check the structure
of the input arguments. For example, check whether |
If use.dimnames = TRUE, then each element's name will be analogous to
paste(rownames(m)[i], colnames(m)[j], sep = sep). If m does not
have rownames and/or colnames, then they will be replaced by dimension positions.
This is also true when m has only one row *and* one column. The exception
is when m has either a single row *or* single column. In these cases,
only the non-single dimension's names will be used. If m has one row,
then the names of the returned vector will be colnames(m). If m
has one column, then the names of the returned vector will be rownames(m).
Again, if m does not have rownames and/or colnames, then they will be
replaced by dimension positions.
(atomic) vector of length = length(m) where the order of elements
from m has been determined by along and the names determined by
the use.dimnames, dimnames(m), and sep. See details for when
use.dimnames = TRUE.
# general matrix
mtcars2 <- as.matrix(mtcars, rownames.force = TRUE) # to make sure dimnames stay in the example
m2v(mtcars2) # default
m2v(m = mtcars2, along = 1) # concatenate along rows
m2v(m = mtcars2, sep = ".") # change the sep of the rownames(m) and colnames(m)
m2v(m = `dimnames<-`(mtcars2, list(NULL, NULL))) # use dimension positions as dimnames
m2v(m = mtcars2, use.dimnames = FALSE) # return object has no names
# one row/column matrix
one_row <- mtcars2[1,, drop = FALSE]
m2v(one_row)
one_col <- mtcars2[, 1, drop = FALSE]
m2v(one_col)
one_all <- mtcars2[1,1, drop = FALSE]
m2v(one_all)
m2v(one_all, use.dimnames = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.