Create a simple non-directed friends graph from list of friends

63 views Asked by At

This is a simple R task. I have a list of some people with IDs and a list of friends of each of the people (with IDs too). They are here:

> dput(friends_of_people)
structure(list(`7614` = c(1091, 1252, 1827, 34687), `29752` = c(1419, 
1799, 3353, 4665), `33220` = c(143, 297, 436, 52078), `34687` = c(14, 
17, 34, 70, 161, 7614), `52078` = c(58, 66, 99, 184, 33220)), .Names = c("7614", 
"29752", "33220", "34687", "52078"))
> dput(people)
c(7614L, 29752L, 33220L, 34687L, 52078L)

I want to extract friend relations from these lists to construct a friends network. For this, I need to create a NxN matrix, where N - the number of people, and 0 in a cell (i,j) means that person i is not a friend of person j, and vice versa (cell j, i, in this case, contains 0 too). If they are friends (there is the ID of person i in the list of friends of person j and vice versa), the cell would contain 1. The final result ought to look like this:

> result
      7614 29752 33220 34687 52078
7614     0     0     0     1     0
29752    0     0     0     0     0
33220    0     0     0     0     1
34687    1     0     0     0     0
52078    0     0     1     0     0

Note that the number of nodes is several thousand in the real task, and a number of friends for each person is also several thousand, so I am worried about the performance. I know this can be an easy task, but don't know where to start. Would appreciate any help.

2

There are 2 answers

0
lukeA On BEST ANSWER

You could also try

edges <- stack(lapply(friends_of_people, intersect, x=people)[as.character(people)])
result <- with(edges, table(factor(values, levels=people), factor(ind, levels=people)))
result
  #       7614 29752 33220 34687 52078
  # 7614     0     0     0     1     0
  # 29752    0     0     0     0     0
  # 33220    0     0     0     0     1
  # 34687    1     0     0     0     0
  # 52078    0     0     1     0     0
1
pogibas On

You can loop through every element in the list and check which entries are in people.

# Matrix filled with 0
# We assume that there's no connection between people
res <- matrix(0, length(people), length(people))
colnames(res) <- rownames(res) <- people

# For every element in list    
for(i in seq_along(friends_of_people)) {
    # Which entries overlap with people vector
    foo <- people %in% friends_of_people[[I]]
    # Change status 
    res[i, which(foo)] <- 1
}

res

enter image description here