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.

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

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)

Value

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

See also

stack_to_matrix() for the reverse operation

Examples

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)
#> 'data.frame':	6150 obs. of  3 variables:
#>  $ species: chr  "Agro.rupe" "Agro.rupe" "Agro.rupe" "Agro.rupe" ...
#>  $ site   : chr  "AR07" "AR71" "AR26" "AR54" ...
#>  $ value  : int  0 0 3 0 0 0 2 0 0 3 ...