Importing edge list in igraph in R - 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).

Related

How to delete an NA (empty) vertex

I'm making an (undirected) social network plot with the igraph package, which I've practised with a subset of my data. For this the vertex have to be in two columns and that way all associations are shown. however sometimes a vertex (individual, I'm working with animals) is encountered alone, with no associations. so this animal will be in the left column and on the right column there is nothing, an empty cell.
However in the igraph package R thinks that "NA"/nothing is an animals ID so it makes a vertex out of it. In my subset I had solved this problem like this:
y <- data.frame(data$ID1, data$ID2)
ID1 and 2 are the codes from the pit-tag readers to recognize the individual animals. its basically their name.
graph.data.frame(y, directed=FALSE)
I'm calling this graph: net
net <- graph.data.frame(y, directed=FALSE)
net <- delete_vertices(net, "")
so in the code shown above the empty values, where nog animal ID was filled in, are deleted from the graph. I was thrilled I achieved this, but as I said it was in a subset of my data which I had already manually edited.
For the whole dataset, I had to wrangle the data. because the animals were observed in larger groups, so I had 8 columns with animal ID which were all associated together. this had to be reformed to two columns in which all possible permutations on a single location was covered (so for a group of 4 animals I needed the combination 1-2; 1-3; 1-4; 2-3; 2-4 and 3-4 and groups vary from 1 to 8 animals(vertexes)). I've done this with the tidyr and dplyr packages (and help). when there's no value in one of the columns (because it was an individual being by itself) R says:
Warning messages:
1: In graph.data.frame(y, directed = FALSE) :
In `d' `NA' elements were replaced with string "NA"
2: In `[<-.factor`(`*tmp*`, thisvar, value = "NA") :
invalid factor level, NA generated
So in my opinion it replaces the empty space with NA, which is also what it show when it tell R to show the new wrangled data. however the trick with remove vertices doesn't work anymore. it keeps saying "invalid vertex name". I've tried it with "", with "NA", NA, "<NA>" and everything logical I could think of but I cannot seem to solve this.
I'm hoping its an error which is easily solvable with a different " or , or something. Anyone have any ideas?

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

How to get a value of a multi-dimensional array by an INCOMPLETE vector of indices

This question is very similar to
R - how to get a value of a multi-dimensional array by a vector of indices
I have:
dim_count <- 5
dims <- rep(3, dim_count)
pi <- array(1:3^5, dims)
I want to get an entire line, but with an automatic building of the address of this line.
For example, I would like to get:
pi[1,,2,2,3]
## [1] 199 202 205
You could insert a sequence covering the whole dimension in the appropriate slot:
do.call("[",list(pi,1,1:dim(pi)[2],2,2,3))
By the way, defining a variable called pi is a little dangerous (I know this was inherited from the previous question) -- suppose you tried a few lines later to compute the circumference of a circle as pi*diameter ...

ROCR Plot using R

I have a csv file (tab deliminated) which contains 2 columns which looks like such
5 0
6 0
9 0
8 1
"+5000 lines similar lines"
I am attempting to create a ROC plot using ROCR.
This is what I have tried so far:
p<-read.csv(file="forROC.csv", head=TRUE, sep="\t")
pred<-prediction(p[1],p[2])
The second line gives me an error: Error in prediction(p[1], p[2]) : Number of classes is not equal to 2.
ROCR currently supports only evaluation of binary classification tasks.
I am not sure what the error is. Is there something wrong with my CSV file?
My guess is that your array indexing isn't setup properly. If you read in that CSV file, you should expect a data.frame (think matrix or 2D array, depending on your background) with two columns and 5,000+ rows.
So your current call to p[1] or p[2] aren't especially meaningful. You probably want to access the first and second column of that data.frame, which you can do using the syntax of p[,1] for the first column and p[,2] for the second.
The specific error you're encountering, however, is a complaint that the "truth" variable you're using isn't binary. It seems that your file is setup to have an output of 1 and 0, so this error may go away once you properly access your array. But if you encounter this in the future, just be sure to binarize your truth data before you use it. For instance:
p[,2] <- p[,2] != 0
Would set the values to FALSE if it's a zero, and TRUE for each non-zero cell in the column.

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