matrixToDataFrame: Convert a Matrix to a Data Frame (in "Long" Format)

View source: R/matrix_to_data_frame.R

matrixToDataFrameR Documentation

Convert a Matrix to a Data Frame (in "Long" Format)

Description

Convert a Matrix to a Data Frame (in "Long" Format)

Usage

matrixToDataFrame(
  x,
  name_row = NULL,
  name_column = NULL,
  name_value = "value",
  row_first = TRUE
)

Arguments

x

matrix

name_row

name to be given to the data frame column containing the row "coordinates". Default: names(dimnames(x))[1] unless NULL, "row" otherwise.

name_column

name to be given to the data frame column containing the column "coordinates". Default: names(dimnames(x))[2] unless NULL, "column" otherwise.

name_value

name to be given to the data frame column containing the matrix values. Default: value

row_first

if TRUE (the default), the "row column" will come first, else the "column column".

Value

data frame with three columns: 1. row "coordinate", 2. column "coordinate", 3. value

Examples

m1 <- matrix(1:12, nrow = 3, dimnames = list(NULL, letters[1:4]))
m2 <- matrix(1:12, nrow = 3, dimnames = list(index = NULL, letters[1:4]))
m3 <- matrix(1:12, nrow = 3, dimnames = list(NULL, letter = letters[1:4]))

matrixToDataFrame(x = m1)
matrixToDataFrame(x = m1, row_first = FALSE)
matrixToDataFrame(x = m2)
matrixToDataFrame(x = m3)
matrixToDataFrame(x = m3, "myrow", "mycol", "myval")


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.