0

I would like to create an adjacency list from a dataset like the following:

id   group
1      1
2      1
3      1
4      2
5      2

The connected id are those who are in the same group. Therefore, I would like to get the following adjacency list:

id   id2
1     2
1     3
2     1
2     3
3     1
3     2
4     5 
5     4

I am struggling in figuring out how to do it. In particular, I have found a solution where order does not matter (split and expand.grid by group on large data set). In my case, it does, so I would not like to have those observations dropped.

A.Tulli
  • 23
  • 4

1 Answers1

0

Maybe something like this, using data.table:

require(data.table)
dt <- fread('id   group
1      1
2      1
3      1
4      2
5      2')

dt[, expand.grid(id, id), by = group][Var1 != Var2][, -1]
#    Var1 Var2
# 1:    2    1
# 2:    3    1
# 3:    1    2
# 4:    3    2
# 5:    1    3
# 6:    2    3
# 7:    5    4
# 8:    4    5
minem
  • 3,640
  • 2
  • 15
  • 29
  • Apologise, I was wrong. I get the following error: "Error in `[.data.frame`(dt, , expand.grid(id, id), by = group) : unused argument (by = group)" . Any idea on what I am doing wrong? – A.Tulli Apr 19 '18 at 15:50
  • @A.Tulli You probably need to convert your data.frame to data.table. Use ‘setDT(dt)’. – minem Apr 19 '18 at 16:14
  • Thanks a lot! I did not understand that your solution was the combination of both the data.table package and expand.grid. – A.Tulli Apr 19 '18 at 16:24