1

I have a matrix ("g") and I'm trying to remove isolates (unconnected nodes). I tried using delete_isolates(g) in the hierformR package, and I tried using in the corpustools package.

Both times I got an error: Error in delete_isolates(g) : could not find function "delete_isolates"

How do I remove unconnected nodes from my matrix?

1 Answers1

2

It is good practice to include a minimal reproducible example of your data. Check How to make a great R reproducible example

With the igraph package you and following example matrix g

library(igraph)
g <-structure(c(0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(5L, 5L), .Dimnames = list(NULL, 
    NULL))

transform your matrix into a graph object

g.old <- graph.adjacency(g, mode = "undirected", diag = FALSE)

and delete nodes with a degree of 0

g.new <- delete.vertices(g.old , which(degree(g.old)==0))
CER
  • 854
  • 10
  • 22