Search code examples
rmatrixnodesigraph

Adding back names after node removal in R


I'm trying to implement this code, based on this solution: R function to remove nodes from a network one by one and outputting the largest connected component

library(igraph)

g <- graph.data.frame(ws, directed=TRUE)

V(g)$names <- paste0("v", 1:length(V(g))) #assign unique name


  testbet <- function(g){
  n <- length(V(g))
  mat <- matrix(ncol=2,nrow=n, 0)
  mat[,1] <- V(g)$names #add vertex name to matrix
  bet <- betweenness(g)
  mat[,2] <- bet
  matri <- mat[order(mat[,2], decreasing = TRUE),]
  g2 <- g
  clustersizes<-integer(n-1)
  for(i in 1:(n-1)){
    g2 <- delete.vertices(g2, v=which(V(g2)$names==matri[i,1])) #index by name
    maxcsize2 <- max(clusters(g2)$csize)
    clustersizes[i]<-maxcsize2
    }
  df<-as.data.frame(cbind(matri, c(clustersizes, NA)))
  names(df)<-c("vertex_removed", "betweenness", "maxcsize")
  return(df)
 }

df<-testbet(g)  

Prior to running this, I define vertice names by

countrycodes <- get.data.frame(g, what= c("vertices"))

I would like to find a way to adding back these names to df.

Reproducible example:

ws <- structure(list(Reporter.Countries = c("Estonia", "Estonia", "Latvia", 
"Uruguay", "Mexico", "Russian Federation", "Lithuania", "China, mainland", 
"Lithuania", "Russian Federation"), Partner.Countries = c("Algeria", 
"Angola", "Algeria", "Algeria", "Algeria", "Afghanistan", "Algeria", 
"Afghanistan", "Angola", "Albania"), Value = c(27150.02, 29001.82, 
57940, 31498.1, 298765.27, 42, 48250, 2564, 41255.03, 143249.35
)), row.names = c(NA, 10L), class = "data.frame")

Solution

  • Quick fix

    library(igraph)
    
    ws <- structure(list(Reporter.Countries = c("Estonia", "Estonia", "Latvia", 
    "Uruguay", "Mexico", "Russian Federation", "Lithuania", "China, mainland", 
    "Lithuania", "Russian Federation"), Partner.Countries = c("Algeria", 
    "Angola", "Algeria", "Algeria", "Algeria", "Afghanistan", "Algeria", 
    "Afghanistan", "Angola", "Albania"), Value = c(27150.02, 29001.82, 
    57940, 31498.1, 298765.27, 42, 48250, 2564, 41255.03, 143249.35
    )), row.names = c(NA, 10L), class = "data.frame")
    
    g <- graph.data.frame(ws, directed=TRUE)
    V(g)$names <- paste0("v", 1:length(V(g))) #assign unique name
    
      testbet <- function(g){
      n <- length(V(g))
      mat <- matrix(ncol=3,nrow=n, 0)                               # <<<<<<<<<<<
      mat[,1] <- V(g)$names #add vertex name to matrix
      bet <- betweenness(g)
      mat[,2] <- bet
      mat[,3] <- V(g)$name                                          # <<<<<<<<<<<
      matri <- mat[order(mat[,2], decreasing = TRUE),]
      g2 <- g
      clustersizes<-integer(n-1)
      for(i in 1:(n-1)){
        g2 <- delete.vertices(g2, v=which(V(g2)$names==matri[i,1])) #index by name
        maxcsize2 <- max(clusters(g2)$csize)
        clustersizes[i]<-maxcsize2
        }
      df<-as.data.frame(cbind(matri, c(clustersizes, NA)))
      names(df)<-c("vertex_removed", "betweenness", "Cn", "maxcsize") #<<<<<<<<<<<<
      return(df)
     }
     
     df <- testbet(g)