在R中使用igraph为图添加属性时的节点匹配与度数异常问题
Let's work through your problems one by one to get your graph set up correctly:
1. Vertex Attribute Misalignment (Birthday Mismatch)
The root cause here is the i + 1 in your loop when accessing the prop dataframe.
Why this happens
V(g)uses 1-based indexing, so the first vertex (i=1) corresponds to the first row of your adjacency matrix (node 27136).- Your
propdataframe is ordered the same way: row 1 = node 27136, row 2 = node 27137, etc. - By using
prop[i + 1, j], you're pulling data from the next row inpropfor each vertex, which shifts all attributes by one position.
The Fix
Replace i + 1 with i, or even better, map attributes using vertex names to avoid issues if node order ever changes:
# First, make sure your graph vertices have the correct names from the adjacency matrix V(g)$name <- rownames(m) # Batch-set attributes using vertex name matching (safer than index-based) for (attr_col in names(prop)) { V(g)[[attr_col]] <- prop[V(g)$name, attr_col] }
2. Incorrect Node Degrees
This is due to how graph.adjacency handles undirected graphs when your adjacency matrix isn't symmetric.
Why this happens
When you use mode="undirected", igraph assumes your adjacency matrix is symmetric. If it's not, igraph only uses the upper triangle (i < j) of the matrix to create edges, ignoring all entries in the lower triangle. This is why your degree counts don't match the full matrix values.
The Fix
You need to symmetricize your adjacency matrix first to ensure all undirected edges are accounted for. Here are two ways to do this:
Option 1: Manually Symmetricize the Matrix
# Add the matrix to its transpose to mirror edges m_sym <- m + t(m) # Remove self-loops (set diagonal to 0) diag(m_sym) <- 0 # Create the undirected graph from the symmetric matrix g <- graph.adjacency(m_sym, mode="undirected", weighted=NULL)
Option 2: Use mode="plus" (Automatic Symmetrization)
igraph's mode="plus" will automatically symmetricize the matrix by adding m and t(m) for you:
g <- graph.adjacency(m, mode="plus", weighted=NULL)
Full Corrected Code
library(igraph) # Load and process adjacency matrix path <- "https://raw.githubusercontent.com/pranavn91/PhD/master/Expt/27022.csv" dat <- read.csv(path, row.names=1, check.names=FALSE, header=T) m <- as.matrix(dat) # Create symmetric undirected graph m_sym <- m + t(m) diag(m_sym) <- 0 g <- graph.adjacency(m_sym, mode="undirected", weighted=NULL) # Load and map vertex attributes path2 <- "https://raw.githubusercontent.com/pranavn91/PhD/master/Expt/27022%20feat.csv" prop <- read.csv(path2, row.names=1, check.names=FALSE, header=T) # Set vertex names and map attributes V(g)$name <- rownames(m) for (attr_col in names(prop)) { V(g)[[attr_col]] <- prop[V(g)$name, attr_col] } # Verify fixes # Check first vertex's birthday cat("First vertex birthday:", V(g)[1]$birthday, "\n") # Check degrees cat("\nNode Degrees:\n") print(igraph::degree(g, v=V(g)))
After these changes, your vertex attributes will align correctly, and the degrees will match your expected values: 27136-2, 27137-2, 27138-1, 27139-3.
内容的提问来源于stack exchange,提问作者pranav nerurkar

