How to fix "Unknown vertex selected" error when deleting vertices using igraph in R

47 Views Asked by At

Using the igraph package with R, I have a large graph with more than 4 million vertices and want to delete vertices where a certain attribute "spatialunit" is NA:

g <- delete.vertices(g, V(g)[is.na(spatialunit)]) or 
g <- delete.vertices(g, V(fullgraph)[is.na(V(g)$spatialunit)])

But get the following error:

Error in simple_vs_index(x, lazy_eval(args[[1]]), na_ok) : Unknown vertex selected

when I create a vertex sequence like vertices_to_delete <- V(g)[is.na(spatialunit)] everything looks fine.

I have tested several workarounds, like adding another attribute with ids as characters, but the problem persists:

V(g)$name <- as.character(seq_len(vcount(g)))
vertex_names_to_delete <- V(g)$name[is.na(V(g)$spatialunit)]
g <- delete.vertices(g, v = vertex_names_to_delete)

Any idea what could cause the problem?

I tried to create a reproducible example, but there it somehow works fine:

library(igraph)
# Create an empty directed graph
g <- graph.empty(n = 0, directed = TRUE)
# Set the number of vertices
num_vertices <- 4176160
# Set the number of vertices with NA "su" attribute
num_na <- 1158722
# Set the attribute values
attribute_values <- c(rep(1, num_vertices - num_na), rep(NA, num_na))
# Add vertices to the graph and assign the "su" attribute
g <- add_vertices(g, nv = num_vertices)
V(g)$su <- attribute_values
g<- delete.vertices(graph = g,v = V(g)[is.na(su)]) 
0

There are 0 best solutions below