R: Convert a Matrix into Tidy Form

tidy_matrix {textshape} | R Documentation

## Convert a Matrix into Tidy Form

### Description

`tidy_matrix` - Converts matrices into a tidy data set. Essentially, a stacking of the matrix columns and repeating row/column names as necessary.

`tidy_adjacency_matrix` - A wrapper for `tidy_matrix` with the `row.name`, `col.name`, & `value.name` all set to `"from"`,`"to"`, & `"n"`, assuming preparation for network analysis.

### Usage

    tidy_matrix(x, row.name = "row", col.name = "col", value.name = "value",
      ...)

    tidy_adjacency_matrix(x, ...)

### Arguments

`x` | A matrix.
`row.name` | A string to use for the row names that are now a column.
`col.name` | A string to use for the column names that are now a column.
`value.name` | A string to use for the values that are now a column.
`...` | ignored.

### Value

Returns a tidied `data.frame`.

### Examples

    mat <- matrix(1:16, nrow = 4,
        dimnames = list(LETTERS[1:4], LETTERS[23:26])
    )

    mat
    tidy_matrix(mat)

    data(simple_dtm)
    tidy_matrix(as.matrix(simple_dtm), 'doc', 'term', 'n')

    X <- as.matrix(simple_dtm[1:10, 1:10])
    tidy_adjacency_matrix(crossprod(X))
    tidy_adjacency_matrix(crossprod(t(X)))

[Package textshape version 1.6.0 Index]