0
votes

I'm new to igraph and struggling a bit with the documentation. I'm trying to query the value of the attributes of a vertex dynamically (without having to hard code the name of the attribute). I need to fetch each attribute one by one using the name of the attribute (not the index) without using adjacency matrix.

require(igraph)
graph <- make_ring(9)  #this is my original graph
V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")  #name of vertices
V(graph)$att1 <- c(1,0,0,0,1,0,0,1,0)
V(graph)$att2 <- c(0,3,1,0,0,1,0,0,1)
V(graph)$att3 <- c(0,0,0,1,4,0,0,0,0)
V(graph)$att4 <- c(1,0,1,0,2,1,0,0,0)
node1 <- V(graph)[1]
node1$att1 # how do I do this without having to hardcode the name of the attribute?

say i'm using a for-loops to query the values of selected attributes of node1:

selected_att <- c("att1", "att3")
for (i in selected_att)
{
   node1$i  #--> how do I get the value of attribute i for node1?
}

To give more context, I'm trying to calculate node similarities between every node in a graph using intra- and inter-coupled attribute similarities to compute the overall similarity between two nodes. Is using adjacency matrix the most efficient way to query the attribute values of each pair of nodes to calculate their overall attribute-coupled similarity? my graph is fairly large so I'm trying to avoid building an adjacency matrix. The result would be a similarity matrix (between every pair of nodes).

1

1 Answers

0
votes

You can't use $ with variables on the right. You can use the more general vertex_attr function. For example you can do

selected_att <- c("att1", "att3")
for (i in selected_att)
{
   print(vertex_attr(graph, i, node1))
}