I'm new to R and igraph and I was wondering if anybody can help me with the following.
I want to find the edge weight between two vertices in a graph. My graph structure is defined by the normal ego (node1), alter (node2) and the weight of the edge between them.
I know that I can get the weight for each of the edges in the list of edges that originate from node number 5 using E(igraph_friendship) [ from(5) ]$weight
And that I can find the weight for each of the edges in the list of edges that end onto node number 10 using E(igraph_friendship) [ to(10) ]$weight
But what if I simply want to find the weight of the edge that simple connects just node 5 and node 10?
Alternatively, if I can get the identifier of the edge that connects node 5 and 10 in the list of all edges, E(igraph_friendship), that would work too.
Thanks a lot for your help, I've been looking around a lot for it and I really appreciate your help!
Gabor's use of the adjacency matrix helped. However, it took me a while to figure out how to get my edge lists with weights into an adjacency matrix. I tried to do it the usual way using graph.data.frame but then would get a weird error when I tried translating the igraph object to and adjacency matrix (error: Error in .M.kind(x) : not yet implemented for matrix w/ typeof character). This post helped do the trick: https://sites.google.com/site/daishizuka/toolkits/sna/weighted-edgelists.
However, what I found out from the R help email list help to work best was this simple operator directly on the igraph object: E(g)[5 %--% 10]$weight. See http://igraph.sourceforge.net/doc/R/iterators.html for details
This is actually quite easy in igraph 0.6 and above, because you can
treat the graph as if it was an adjacency matrix (or weighed adjacency
matrix in your case):
library(igraph)
g <- graph.ring(10)
g[1,2]
# [1] 1
E(g)$weight <- runif(ecount(g))
g[1,2]
# [1] 0.8115639
If you want to do this for the whole matrix, then you can simply do
g[]
Or, if you don't want sparse matrices, then
g[sparse=FALSE]
Please see ?"[.igraph" for more.
Related
I recently began working on r for social network analysis. Everything goes well and up until now, I found answers to my questions here or on google. But not this time!
I am trying to find a way to calculate "vertex reciprocity" (% of reciprocal edges of each actor of the network). On igraph, reciprocity(g) works fine to calculate the reciprocity of the whole network, but it doesn't help me with the score per actor. Does anybody know what I could do?
Thank you!
I am going to assume that you have a simple graph, that is no loops and no multiple links between nodes. In that case, it is fairly easy to compute this. What does it mean for a link to be reciprocated? When there is a link from a to b, there is a link back from b to a. That means that there is a path of length two from a to itself a->b->a. How many such paths are there? If A is the adjacency matrix, then the entries of AA gives the number of paths of length two. We only want the ones from a node to itself, so we want the diagonal of AA. This will only count a->b->a as one path, but you want to count it twice: once for the link a->b and once for b->a. So for each node you can get the number of reciprocated links from 2*diag(A*A). You want to divide by the total number of links to and from a which is just the degree.
Let me show the computation with an example. Since you do not provide any data, I will use the Enron email data that is available in the 'igraphdata' package. It has loops and multiple links which i will remove. It also has a few isolated vertices, which I will also remove. That will leave us with a connected, directed graph with no loops.
library(igraph)
library(igraphdata)
data(enron)
enron = simplify(enron)
## remove two isolated vertices
enron = delete_vertices(enron, c(72,118))
Now the reciprocity computation is easy.
EnronAM = as.matrix(as_adjacency_matrix(enron))
Path2 = diag(EnronAM %*% EnronAM)
degree(enron)
VertRecip = 2*Path2 / degree(enron)
Let's check it by walking through one node in detail. I will use node number 1.
degree(enron,1)
[1] 10
ENDS = ends(enron, E(enron))
E(enron)[which(ENDS[,1] == 1)]
+ 6/3010 edges from b72ec54:
[1] 1-> 10 1-> 21 1-> 49 1-> 91 1->104 1->151
E(enron)[which(ENDS[,2] == 1)]
+ 4/3010 edges from b72ec54:
[1] 10->1 21->1 105->1 151->1
Path2[1]
[1] 3
Node 1 has degree 10; 6 edges out and 4 edges in. Recip shows that there are three paths of length 2 from 1 back to itself.
1->10->1
1->21->1
1->151->1
That makes 6 reciprocated links and 4 unreciprocated links. The vertex reciprocity should be 6/10 = 0.6 which agrees with what we computed above.
VertRecip[1]
[1] 0.6
I am unable to continue writing the code down below because of the ends(graph, es, names = TRUE) function (the description).
I installed igraph library and I verified from everything but the ends function keep giving me an error.
> library(igraph)
> setwd("Desktop")
> file <- "distance"
> con <- file(description=file, open="r")
> line <- read.table(con)
> data<-as.data.frame(line)
> df <- graph.data.frame(d = data, directed = FALSE) #to convert data to a graph object
> edge<-sample(E(df),1) # sample an edge randomly
> edge
Edge sequence:
e
e [16567] 5578 -- 6774
> ends(graph = g, es = 'e')[2] #get the second vertex for edge e
Error in ends(graph = g, es = "e") : could not find function "ends"
The file "distance" contain the data which it is a set of edges ordered in two columns, each row is an edge and each value in the column represent a vertex as:
1 2
2 3
3 4
so 1 2 is an edge between the vertices 1 & 2.
I want this function to get the incident vertices of a randomly selected edge, I searched the interent and R libraries but I a can't find a similar function or something to do the same which allow me to select a certain vertex from an edge, here is a similar problem link but the proposed solution is to use ends().
Could you kindly tell me why I am unable to use this function or to propose another function for the same purpose.
Many thanks in advance
EDIT
It seems that the problem is the version of igraph !! the ends(graph,..) is not defined in this version.
My question now,
Because it is impossible to upgrade the igraph version, is there other functions to select a certain vertex from an edge?
Thanks
Just to help who would face the same problems..
my aim was to read the edges by vertices so that I can call any vertex in an edge, so I transformed the data from igraph object to edgelist by adding the following lines
edges<-get.edgelist(df)
v1<-edges[edge,1]
v2<-edges[edge,2]
edge
Edge sequence:
e
e [8839] 1149 -- 1425
v1
[1] "1425"
v2
[1] "1149"
It is not a great solution but it solved my problem so maybe it could help other beginners like me
My question is similar to the one posted here: Network adding edges error
I am creating a network from scratches: I have data about 228 vertices, over a period of 13 years. In the first year, I have just 1781 edges: they do not involve all of my vertices (barely 164), therefore the remaining nodes should result as isolated.
I created the network starting from my edgelist, using the code
fdi.graph.2003 <- graph_from_data_frame(fdi.edge.2003, directed = T, vertices = fdi.attr.2003)
where fdi.edge.2003 is a data.frame containing edge attributes (including some potential weight columns): it only involves 164 vertices out of the total vertices defined by fdi.attr.2003 is a data.frame containing a row for each vertex that is involved in the edgelist (164 in total).
all i get is a network with 164 vertices and no isolates. However, I know they do exist in my data! Any suggestion on how to do it? I think that I shoul initialize a network with all 228 vertices, adding their attributes and then adding the edges. However, nothing I am trying is working: rather, I am receiving the most disparate errors related to "Illegal vertex reference in addEdges_R".
Any suggestion is more than welcome, also in the case it would involve the alternative package igraph, for which I am finding the same problem
Filippo
Use add.isolates from the sna package
net1 = as.network(cbind(1:3, 3:5)) #5 vertices, 3 edges
net2 = as.network(add.isolates(net1, 10), matrix.type = "edgelist") #15 v, 3 e
And then you'll probably want to create new vertex names, e.g.
net2%v%"vertex.names" = 1:15
I am new to igraph and graph theory. I have a very large file (> 4 GB) and I was told it is a graph. I can see the format includes the pairs separated by tab and I can read it as a table first then convert it to graph data frame.
Number of vertices with vcount and number of edges with ecount suggest that there are vertices with multiple edges. I have been looking at various sources but I could not find the information about directly extracting the vertices with more than one edges.
Any help is appreciated.
To get the edges incident to each vertex (if g is your igraph)
ie <- igraph::incident_edges(g, igraph::V(g))
Then, to get the number of edges adjacent to each vertex
num.incident.edges <- sapply(ie, length)
Sorry, I guess I was wrong with the terminology. What I meant by vertices with multiple edges is called 'articulation_points'.
This was what I was looking for:
library(igraph)
bi <- biconnected_components(g)
bi$articulation_points
I'm trying to import an edge list into igraph's graph object in R. Here's how I'm trying to do so:
graph <- read.graph(edgeListFile, directed=FALSE)
I've used this method before a million times, but it just won't work for this specific data set:
294834289 476607837
560992068 2352984973
560992068 575083378
229711468 204058748
2432968663 2172432571
2473095109 2601551818
...
R throws me this error:
Error in read.graph.edgelist(file, ...) :
At structure_generators.c:84 : Invalid (negative) vertex id, Invalid vertex id
The only difference I see between this dataset and the ones I previously used is that those were in sorted form, starting from 1:
1 1
1 2
2 4
...
Any clues?
It seems likely that it's trying to interpret the values as indexes rather than node names and it's probably storing them in a signed integer field that is too small and is probably overflowing into negative numbers. One potential work around is
library("igraph")
dd <- read.table("test.txt")
gg <- graph.data.frame(dd, directed=FALSE)
plot(gg)
It seems this method doesn't have the overflow problem (assuming that's what it was).