How to delete vertices in graph in R? - r

I’m trying to remove the vertex in a graph with delete.vertices(graph ,vertex id) but I see this error: ”Cannot create iterator, Invalid vertex id, Invalid vertex id”
This is my code:
graph<-read.graph(paste0("H:/graph.txt"),format="ncol",directed="TRUE")
delete.vertices(graph,c(717527,61373))
717527 and 61373 are my vertices number.
and this is the graph:
How can I do this?

You get the error because one of the vertices given is not in the list of the graph vertices. One advise try to play with igraph functions, and once your are familiar with them apply it to your custom graph. Here a demonstration:
library(igraph)
g <- graph.ring(10)
V(g)
Vertex sequence:
[1] 1 2 3 4 5 6 7 8 9 10
Now I try to delete an existing vertex:
delete.vertices(g, c(2,7,9) )
IGRAPH U--- 7 4 -- Ring graph
+ attr: name (g/c), mutual (g/x), circular (g/x)
For non existing one , I get your error:
delete.vertices(g, c(20))
Error in delete.vertices(g, c(20)) :
At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id

Are you using Igraph?
the read.graph function may be reading vertex id's in as strings. In that case you may want:
delete.vertices(graph,c('717527','61373'))
instead. Also, delete.verticies returns a new graph object. You want to make sure to store the result in your graph if you want to see the changes show up latter.
graph<-read.graph(paste0("H:/graph.txt"),format="ncol",directed="TRUE")
graph<-delete.vertices(graph,c('717527','61373'))

Related

The function "ends" is not exist although igraph was installed

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

Extract just the number of edges in a class graphNEL

Using R I have created an graphNEL class (undirected) with nodes and edges. I want to save the numbers that gets printed out when I print the variable which the undirected graph is saved under:
graphNEL graph with undirected edges
Number of Nodes = 671
Number of Edges = 4267
I tried using the function edgeL(), but the number I get printed out is the number of nodes. I was thinking that I get this number because each gene has its own amount of edges, which is why the output equals the number of nodes. All I want is to save the number of edges in this graph. How can I do this?
Thanks
I am not familiar with the package, however it looks like nodes and edgeL are returned as slots. This means that you should be able to see the data using $ or # if you save your graphNEL as an object.
yourgraphObject#edgeL

Importing edge list in igraph in R

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).

igraph edge between two vertices

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.

How to copy a vertex with it's respective edges (all/in/out) from a directed graph g, to a new directed graph g1?

Is there a method or a class in igraph to do this procedure fast and efectively?
Let's assume that your graph is in g and the set of vertices to be used is in sampled (which is a vector consisting of zero-based vertex IDs).
First, we select the set of edges where at least one endpoint is in sampled:
all.vertices <- (1:vcount(g)) - 1
es <- E(g) [ sampled %--% 1:n ]
es is now an "edge sequence" object that consists of the edges of interest. Next, we take the edge list of the graph (which is an m x 2 matrix) and select the rows corresponding to the edges:
el <- get.edgelist(g)[as.vector(es)+1]
Here, as.vector(es) converts the edge sequence into a vector consisting of the edge IDs of the edges in the edge sequence, and use it to select the appropriate subset of the edge list. Note that we had to add 1 to the edge IDs because R vectors are indexed from 1 but igraph edge IDs are from zero.
Next, we construct the result from the edge list:
g1 <- graph(el, vcount(g), directed=is.directed(g))
Note that g1 will contain exactly as many vertices as g. You can take the subgraph consisting of the sampled vertices as follows:
g1 <- subgraph(g1, sampled)
Note to users of igraph 0.6 and above: igraph 0.6 will switch to 1-based indexing instead of 0-based, so there is no need to subtract 1 from all.vertices and there is no need to add 1 to as.vector(es). Furthermore, igraph 0.6 will contain a function called subgraph.edges, so one could simply use this:
g1 <- subgraph.edges(g, es)

Resources