Learn R Programming

Rnets (version 1.2.1)

Sq2Long: Matrix reshaping function

Description

This function resphapes all or parts of a square matrix into a 3 column table.

Usage

Sq2Long(x, col.names = NULL, keep = c(T, T, T), drop.values = 0,
  drop.NA = T)

Arguments

x

A square matrix to reshape into a dataframe.

col.names

A vector indicating how output columns are named.

keep

A logical vector of length 3 indicating which of the three respective elements to keep: upper triangle, main diagonal, and lower triangle.

drop.values

A vector of values to remove from the output.

drop.NA

Logical value indicating if NAs should be removed.

Value

a data.frame with three columns. Columns 1 & 2 contain the row & column names, and third contains corresponding matrix values.

Examples

Run this code
# NOT RUN {
demo_mat <- matrix(c(1, 5, 2, 5, 1, 0, 2, 0, NA), nrow = 3)
Sq2Long(demo_mat, c('A', 'B', 'value'))
Sq2Long(demo_mat, c('A', 'B', 'value'), drop.values = 0, drop.NA = FALSE)
# }

Run the code above in your browser using DataLab