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

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/matrix2longdf.R

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

1
2
3
4
5
6
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

Details

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

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.

Note

The length of row.names and col.names should be as the same as the matrix dimension. Otherwise the function raises warnings.

Author(s)

Jitao David Zhang <jitao_david.zhang@roche.com>

Examples

1
2
3
4
5
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")))

ribiosUtils documentation built on March 13, 2020, 2:54 a.m.