Learn R Programming

funrar (version 1.5.0)

matrix_to_stack: Matrix to stacked (= tidy) data.frame

Description

From a matrix with values to a stacked (= tidy) data.frame, exclude NA from given data.frame. If supplied object is not a matrix, try to coerce object to matrix first. matrix_to_tidy() is an alias of this function.

Usage

matrix_to_stack(
  my_mat,
  value_col = "value",
  row_to_col = names(dimnames(my_mat))[1],
  col_to_col = names(dimnames(my_mat))[2]
)

Value

a stacked (= tidy) data.frame with, a column for row names, one for column names and a third one for the values.

Arguments

my_mat

matrix you want to transform in stacked (= tidy) data.frame

value_col

(optional) character vector to use for value column (default: 'value')

row_to_col

(optional) character vector used for name of column in data.frame corresponding to rows in matrix (default: corresponding dimension name)

col_to_col

(optional) character vector used for name of column in data.frame corresponding to columns in matrix (default: corresponding dimension name)

See Also

stack_to_matrix() for the reverse operation

Examples

Run this code
data("aravo", package = "ade4")

# Site-species matrix converted into data.frame
mat = as.matrix(aravo$spe)
dat = matrix_to_stack(mat, "value", "site", "species")
str(dat)

Run the code above in your browser using DataLab