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

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

Related

How to create network with both edges and isolates using statnet/igraph

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

R iGraph: is the first vertex always the central one in neighborhood?

I have noticed that, when calling the function neighborhood, the first vertex in the list is always the central one (the vertex of interest). See for instance:
g <-graph_from_literal( a-b,a-c,d-a,a-e,a-f,b-h,d-e,c-i )
nn <- neighborhood( g, order=1, nodes=V(g) )
# > nn[[1]]
# + 6/8 vertices, named:
# [1] a b c d e f
In other words, when calculating the neighborhood of node "a", neighborhood reports "a" first and then the nearest neighbors and so on.
I'd like to know if this order is not a coincidence and it's a true characteristic of the function that I can safely use in my program. I didn't find an answer in the documentation.
Well, it is a characteristic of the current implementation of the function, so right now you can rely on it. However, since it is not a part of the documentation, the authors of the package are free to change the behaviour in the next version without warning.

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

How to delete vertices in graph in 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'))

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.

Resources