# R Network Analysis

> R network analysis with igraph, sna. Use for centrality, community detection, and network metrics.

- Skill: `leolin990405/r-network-analysis` (Agent Skill)
- Install (CLI): `npx get-skill add leolin990405/r-network-analysis`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leolin990405/r-network-analysis/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: LeoLin990405 (https://skillmd.com/u/leolin990405)
- Updated: 2026-11-14
- Page: https://skillmd.com/skills/leolin990405/r-network-analysis

---


# R Network Analysis

Graph analysis and metrics.

## igraph

```r
library(igraph)

# Create graph
g <- graph_from_data_frame(edges, directed = TRUE, vertices = nodes)
g <- graph_from_adjacency_matrix(adj_matrix)
g <- make_ring(10)

# Basic info
vcount(g); ecount(g)
V(g); E(g)
is_directed(g)

# Centrality
degree(g)
betweenness(g)
closeness(g)
eigen_centrality(g)$vector
page_rank(g)$vector

# Community detection
cluster_louvain(g)
cluster_walktrap(g)
cluster_edge_betweenness(g)
membership(communities)
modularity(communities)

# Paths
shortest_paths(g, from = 1, to = 10)
distances(g)
diameter(g)

# Network metrics
transitivity(g)  # Clustering coefficient
graph.density(g)
assortativity_degree(g)
```

## sna

```r
library(sna)

# Centrality
degree(net, cmode = "indegree")
betweenness(net)
closeness(net)

# Structural equivalence
equiv.clust(net)

# QAP correlation
qaptest(list(net1, net2), gcor, g1 = 1, g2 = 2)
```

## Network Statistics

```r
# Degree distribution
degree_distribution(g)
fit_power_law(degree(g))

# Clustering
transitivity(g, type = "local")
transitivity(g, type = "global")

# Components
components(g)
largest_component <- decompose(g)[[1]]

# Motifs
motifs(g, size = 3)
count_motifs(g, size = 3)
```

