Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple conversion to edgelist with R?

I need to do a simple data conversion in R for usage with igraph. My dataframe is in this format, grouped by GROUP:

    A   GROUP
1   1       a
2   2       a
3   3       a
4   4       a
5   1       b
6   3       b
7   5       b

1. How do I expand the groups to get an undirected edgelist el in this format?

    A   B
1   1   2
2   1   3
3   1   4
4   2   3
5   2   4
6   3   4
7   1   3
8   1   5
9   3   5

Note: no self-references 1-1, 2-2, 3-3, ...

2. How do I count A-B occurrences and create a weighted edgelist from el?

    A   B   weight
1   1   2        1
2   1   3        2
3   1   4        1
4   2   3        1
5   2   4        1
6   3   4        1
7   1   5        1
8   3   5        1
like image 990
R-obert Avatar asked Mar 24 '12 09:03

R-obert


2 Answers

Here is a solution, I commented in the code:

# your data
df <- data.frame(A = c(1, 2, 3, 4, 1, 3, 5),
             GROUP = c("a", "a", "a", "a", "b", "b", "b"))

# define a function returning the edges for a single group
group.edges <- function(x) {
  edges.matrix <- t(combn(x, 2))
  colnames(edges.matrix) <- c("A", "B")
  edges.df <- as.data.frame(edges.matrix)
  return(edges.df)
}

# apply the function above to each group and bind altogether
all.edges <- do.call(rbind, lapply(unstack(df), group.edges))

# add weights
all.edges$weight <- 1
all.edges <- aggregate(weight ~ A + B, all.edges, sum)
all.edges
#   A B weight
# 1 1 2      1
# 2 1 3      2
# 3 2 3      1
# 4 1 4      1
# 5 2 4      1
# 6 3 4      1
# 7 1 5      1
# 8 3 5      1
like image 63
flodel Avatar answered Oct 10 '22 02:10

flodel


Here is a way to get the edgelist with plyr:

foo <- data.frame(
  A = c(1,2,3,4,1,3,5),   
  GROUP = c("a","a","a","a","b","b","b"))

library("plyr")

E1 <- do.call(rbind,dlply(foo,.(GROUP),function(x)t(combn(x$A,2))))

E1

Returns:

      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    2    3
 [5,]    2    4
 [6,]    3    4
 [7,]    1    3
 [8,]    1    5
 [9,]    3    5

Then to get the weights (here I use that combn puts the lowest number first):

W <- apply(E1,1,function(x)sum(E1[,1]==x[1]&E1[,2]==x[2]))
E2 <- cbind(E1,weight=W)
E2 <- E2[!duplicated(E2),]

E2

Which returns:

         weight
[1,] 1 2      1
[2,] 1 3      2
[3,] 1 4      1
[4,] 2 3      1
[5,] 2 4      1
[6,] 3 4      1
[7,] 1 5      1
[8,] 3 5      1
like image 38
Sacha Epskamp Avatar answered Oct 10 '22 00:10

Sacha Epskamp