Network error message Rstudio using igraph

Viewed 278

I am trying to make a network with these data points. I have them organized so the sources go to their proper target. However, I run into an error when I run,

library(igraph)


links <- data.frame(
  source=c("P","P","P","P","G","G","T","T","N","N","B","B","S","S","O","D"),
  target=c("G","B","S","D","T","D","N","D","R","D","R","D","B","E","E","E"),
  importance=(sample(1:4, 16, replace=T))
  )

nodes <- data.frame(
  name=LETTERS[1:10],
  carac=c( rep("bio",2),rep("affective",3),rep("cog",3),rep("neg",2))
  )


network <- graph_from_data_frame(d=links, vertices=nodes, directed=F) 

The error I get happens on the last line. It says

Error in graph_from_data_frame(d = links, vertices = nodes, directed = F) : Some vertex names in edge list are not listed in vertex data frame

I am not understanding what is wrong with my vertex data frame. I also do not know what the "edge list" is that it is referring to. Any help debugging would be appreciated!

This is base code from r-graph-gallery that I am plugging my data into. It is supposed to create a network with links and nodes. I don't understand what replace or importance refers to, and it isn't explained on the website. This is what the base data looks like.

    library(igraph)
 
links <- data.frame(
    source=c("A","A", "A", "A", "A","J", "B", "B", "C", "C", "D","I"),
    target=c("B","B", "C", "D", "J","A","E", "F", "G", "H", "I","I"),
    importance=(sample(1:4, 12, replace=T))
    )
nodes <- data.frame(
    name=LETTERS[1:10],
    carac=c( rep("young",3),rep("adult",2), rep("old",5))
    )
 
network <- graph_from_data_frame(d=links, vertices=nodes, directed=F)
1 Answers
library(igraph)

 
links <- data.frame(  
 
source=c("P","P","P","P","G","G","T","T",
"N","N","B","B","S","S","O","D"),
 
target=c("G","B","S","D","T","D","N","D",
"R","D","R","D","B","E","E","E"),
 importance=(sample(1:4, 16, replace=T)) ) 

V = c(links$source,
 links$target) %>% 
unique() 

nodes <- data.frame(   name=V,   
carac=c(
 rep("bio",2),rep("affective",3),rep("cog",3),rep("neg",2)) )
 
 
network <- graph_from_data_frame(d=links, vertices=nodes, directed=F) 
 
plot(network)

You need to make sure all your node IDs exist in the Node dataframe. They should be all nodes that exist in the link df.

Related