Learn R Programming

cograph (version 1.5.2)

as_cograph: Convert to Cograph Network

Description

Creates a lightweight cograph_network object from various network inputs. The resulting object is a named list with all data accessible via $.

Usage

as_cograph(x, directed = NULL, ...)

Value

A cograph_network object: a named list with components:

from

Integer vector of source node indices

to

Integer vector of target node indices

weight

Numeric vector of edge weights

nodes

Data frame with id, label, (x, y if layout applied)

directed

Logical indicating if network is directed

n_nodes

Integer count of nodes

n_edges

Integer count of edges

labels

Character vector of node labels

source

Character indicating input type

layout

Layout coordinates (NULL until computed)

layout_info

Layout algorithm info (NULL until computed)

Arguments

x

Network input. Can be:

  • A square numeric matrix (adjacency/weight matrix)

  • A data frame with edge list (from, to, optional weight columns)

  • An igraph object

  • A statnet network object

  • A qgraph object

  • A tna object

  • An existing cograph_network object (returned as-is)

directed

Logical. Force directed interpretation. NULL for auto-detect.

...

Additional arguments (currently unused).

Details

The cograph_network format is designed to be:

  • Simple: All data accessible via net$from, net$to, net$weight, etc.

  • Modern: Uses named list elements instead of attributes for clean $ access

  • Compatible: Works seamlessly with splot() and other cograph functions

Use getter functions for programmatic access: get_nodes, get_edges, get_labels

Use setter functions to modify: set_nodes, set_edges, set_layout

See Also

get_nodes to extract the nodes data frame, get_edges to extract edges as a data frame, n_nodes and n_edges for counts, is_directed to check directedness, splot for plotting

Examples

Run this code
# From adjacency matrix
mat <- matrix(c(0, 1, 1, 1, 0, 1, 1, 1, 0), nrow = 3)
net <- as_cograph(mat)

# Direct $ access to all data
net$from       # edge sources
net$to         # edge targets
net$weight     # edge weights
net$nodes      # nodes data frame
net$directed   # TRUE/FALSE
net$n_nodes    # 3
net$n_edges    # 3

# Getter functions (recommended for programmatic use)
get_nodes(net)   # nodes data frame
get_edges(net)   # edges data frame (from, to, weight)
get_labels(net)  # character vector of labels
n_nodes(net)     # 3
n_edges(net)     # 3
is_directed(net) # FALSE (symmetric matrix)

# Setter functions
net <- set_nodes(net, data.frame(id = 1:3, label = c("A", "B", "C")))
net <- set_edges(net, data.frame(from = c(1,2), to = c(2,3), weight = c(0.5, 0.8)))
net <- set_layout(net, data.frame(x = c(0, 1, 0.5), y = c(0, 0, 1)))

# Plot it
splot(net)

# From igraph (if installed)
if (requireNamespace("igraph", quietly = TRUE)) {
  library(igraph)
  g <- make_ring(10)
  net <- as_cograph(g)
  splot(net)
}

Run the code above in your browser using DataLab