Search code examples
rgraphextractvertex

extract vertex from a R graph


I have the following problem.
A directed graph called tutti
I have a vector called tabellaerrori containing a vertex in each position
Now my problem is:
I want to create an array cointaining the list of vertex which are both in tutti graph and in errori vector.
I used the following code but it doesn't work:

risultato<-as.character(intersect(tabellaerrori,V(tutti)))

It gives me back always the content of tabellaerrori What's wrong ?


Solution

  • For those who didn't suffer through the non-downloadable google plus image gallery, the actual line generating the error is:

    graph.neighborhood(tutti, vcount(tutti), risultato, "out")
    ## Error in as.igraph.vs(graph, nodes) : Invalid vertex names
    

    From the help - graph.neighborhood(graph, order, nodes=V(graph), mode=c("all", "out", "in")) is expecting nodes to be an actual igraph vertex sequence. You just need to make sure your intersected nodes are in that form.

    Here's what jbaums meant by a reproducible example (provided I've made the right assumptions from your screen captures):

    library(igraph)
    
    set.seed(1492) # makes this more reproducible
    
    # simulate your overall graph
    
    tutti <- graph.full(604, directed=TRUE)
    V(tutti)$name <- as.character(sample(5000, 604))
    
    # simulate your nodes
    
    tabellaerrori <- as.character(c(sample(V(tutti), 79), sample(6000:6500, 70)))
    names(tabellaerrori) <- as.numeric(tabellaerrori)
    
    # give a brief view of the overall data objects
    
    head(V(tutti))
    ## Vertex sequence:
    ## [1] "1389" "1081" "922"  "553"  "261"  "42"
    
    length(V(tutti))
    ## [1] 604
    
    head(tabellaerrori)
    ##   293   415   132   299   408   526 
    ## "293" "415" "132" "299" "408" "526"
    
    length(tabellaerrori)
    ## [1] 149
    
    # for your answer, find the intersection of the vertext *names*
    
    risultato <- as.character(intersect(tabellaerrori, V(tutti)$name))
    risultato
    ## Vertex sequence:
    ## [1] "293" "132" "155" "261" "68"  "381" "217" "394" "581"
    
    # who are the ppl in your neighborhood
    
    graph.neighborhood(tutti, vcount(tutti), risultato, "out")
    ## [[1]]
    ## IGRAPH DN-- 604 364212 -- Full graph
    ## + attr: name (g/c), loops (g/l), name (v/c)
    ## 
    ## [[2]]
    ## IGRAPH DN-- 604 364212 -- Full graph
    ## + attr: name (g/c), loops (g/l), name (v/c)
    ##
    ## ... (a few more)
    

    What you were really doing before (i.e. what intersect was doing under the covers) is:

    risultato <- as.character(intersect(tabellaerrori, as.character(V(tutti))))
    

    hence, your Invalid vertex names error.