Learn R Programming

ribiosUtils (version 1.5-6)

matrix2longdf: Transform a matrix into a long-format data.frame

Description

The function converts a matrix into a long-format, three-column data.frame, containing row, columna nd value. Such ‘long’ data.frames can be useful in data visualization and modelling.

Usage

matrix2longdf(
  mat,
  row.names,
  col.names,
  longdf.colnames = c("row", "column", "value")
)

Arguments

mat

A matrix

row.names

Character, row names to appear in the data.frame. If missing, the rownames of the matrix will be used. If set to NULL, or if the matrix rownames are NULL, a integer index vector starting from 1 will be used.

col.names

Charater, column names to appear in the data.frame. The rule of handling missing or NULL parameters is the same as row.names described above.

longdf.colnames

Character, column names of the output long data frame

Value

A data.frame object with three columns: row, column and value. If the input matrix is of dimesion MxN, the returning data.frame is of the dimension MNx3.

Details

The function converts a matrix into a three-column, ‘long’ format data.frame containing row names, column names, and values of the matrix.

Examples

Run this code
# NOT RUN {
test.mat <- matrix(1:12, ncol=4, nrow=3, dimnames=list(LETTERS[1:3],
LETTERS[1:4]))
print(test.mat)
print(matrix2longdf(test.mat))
print(matrix2longdf(test.mat, longdf.colnames=c("From", "To", "Time")))

# }

Run the code above in your browser using DataLab