convert_to_matrix: Convert Input to Adjacency Matrix

View source: R/convert_inputs.R

convert_to_matrixR Documentation

Convert Input to Adjacency Matrix

Description

Converts various input formats (data.frame, matrix) to a standard adjacency matrix suitable for ISM analysis. Performs validation and provides warnings for potential issues.

Usage

convert_to_matrix(x, from = 1, to = 2, nodes = NULL, validate = TRUE)

Arguments

x

Input data. Can be:

  • A two-column matrix representing an edge list

  • A square matrix (adjacency matrix)

  • A data.frame with source and target columns

from

Column name or index for source nodes (if x is a data.frame). Default is 1.

to

Column name or index for target nodes (if x is a data.frame). Default is 2.

nodes

Optional character vector of predefined node names. If provided, ensures the resulting matrix includes all specified nodes.

validate

Logical. If TRUE (default), validates the input and provides warnings for potential issues.

Details

This function provides flexible input handling for ISM analysis. It can convert edge lists (either as data frames or two-column matrices) into adjacency matrices, or validate and return existing adjacency matrices.

When validate = TRUE, the function checks:

  • Square matrices contain only 0s and 1s (or logical values)

  • Edge list nodes exist in the predefined node list (if provided)

  • Column names exist in data frames

Value

A square numeric adjacency matrix with node names as row and column names. A value of 1 at position (i,j) indicates a directed edge from node i to node j.

See Also

create_relation_matrix for a higher-level interface, ssim_to_matrix for SSIM conversion, compute_reachability for computing reachability matrices.

Examples

# From data frame edge list
edge_df <- data.frame(source = c("A", "B"), target = c("B", "C"))
convert_to_matrix(edge_df, from = "source", to = "target")

# From matrix edge list
edge_mat <- matrix(c("A", "B", "B", "C"), ncol = 2, byrow = TRUE)
convert_to_matrix(edge_mat)

# Existing adjacency matrix (validated and returned)
adj_mat <- matrix(c(0, 1, 0, 0,
                    0, 0, 1, 1,
                    0, 0, 0, 0,
                    0, 0, 0, 0), nrow = 4, byrow = TRUE)
convert_to_matrix(adj_mat)

# With predefined nodes (ensures all nodes are included)
edge_df <- data.frame(source = "A", target = "B")
convert_to_matrix(edge_df, from = "source", to = "target",
                  nodes = c("A", "B", "C", "D"))

ISMtools documentation built on March 13, 2026, 1:06 a.m.