Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting nodes according to degree and manipulating in R using igraph

the datasets I am working with shows links between nodes.For Example:

> data2
   V1   V2
1 10000 4725
2  4725 6805
3  4725 3250
4  5725 3250
5  1725 7673

(Using a small dataframe for example).Here the dataframe says that there is an undirected link between node 10000 and 4725,a link exists between node 4725 and 6805 and so forth.Using igraph package,I am obtaining the degree for individual nodes:

  g<-graph.data.frame(data2, directed=F)
 deg <- igraph::degree(g)
> deg
   10000  4725  5725  1725  6805  3250  7673 
    1     3     1     1     1     2     1 

Next,I am sorting the nodes according to their degrees in decreasing order:

 > dSorted <-sort.int(deg,decreasing=TRUE,index.return=FALSE)
 > dSorted
 4725  3250 10000  5725  1725  6805  7673 
   3     2     1     1     1     1     1

Taking the first column of the dataframe:

  > ln1 <- data2[,1]
> ln1
[1] 10000  4725  4725  5725  1725

My objective is to replace the nodes in ln1 with the nodes' corresponding orders in dSorted.For example,10000 should be replaced with 3,because in dSorted 10000 comes at 3rd index.Similarly 4725 should be replaced with 1,because it comes first in dSorted.I have tried the following code:

> for(i in 1:length(deg)){ln1[which(ln1==dSorted[i])]<-i}

But it is not working.ln1 stays the same.It occurred to me that in dSorted,the node numbers are being considered as indexes.So I tried the following code as well(dSorted returning an index vector):

> dSorted <- sort.int(deg,decreasing=TRUE,index.return=TRUE)
> for(i in 1:length(deg)){ln1[which(ln1==dSorted$ix[i])]<-i}

But ln1 still stays the same.I am very new with R.Would really appreciate if someone kindly shows me a path here.

like image 590
Kitne Avatar asked Nov 10 '22 09:11

Kitne


1 Answers

If I understand you correctly, you can do:

ln1 <- order(order(deg, decreasing=T))
# [1] 3 1 4 5 6 2 7

# if you want names
names(ln1) <- names(deg)
# 10000  4725  5725  1725  6805  3250  7673 
#     3     1     4     5     6     2     7

So as mentioned, 10000 has value 3 because it is 3rd in the order 4725 has value 1 because it is 1st in the order, etc.

The way sort and order are related: sort by default sorts your vector, and order returns the indices that sort your vector.

Why the double-order? they are kind of inverses of each other.

sorted <- sort(deg)
deg[order(deg)] == sorted
sorted[order(order(deg))] == deg

order(deg) will arrange your unsorted deg so that it is in order. order(order(deg)) will arrange your sorted deg so it resembles the original order. Confusing in words, but play around with it and you'll see.

like image 162
mathematical.coffee Avatar answered Nov 15 '22 06:11

mathematical.coffee