0

Let's say I have a list of strings:

list <- c("john", "carl", "suzanne")

Because they appear in the same list, I'm making a social network assumption that they are connected. In order to be able to convert this group to a graph, I need to make it to an edgelist:

from to
john carl
carl suzanne
suzanne john

The group, however, may contain 1 to infinity number of strings. How do I construct a function that convert such a list of strings to an edgelist?

Spontaneously it seems that I can assign each string to a generic object (say, name, name2, and name3), and then just create a matrix as follows:

name1 = "john"
name2 = "carl"
name3 = "suzanne"
matrix(data = c(name1, name2, name2, name3, name1, name3), byrow=TRUE,
ncol=2)

     [,1]   [,2]     
[1,] "john" "carl"   
[2,] "carl" "suzanne"
[3,] "john" "suzanne"

However, this solution doesn't scale very well. How can I make a scaleable solution?

histelheim
  • 4,938
  • 6
  • 33
  • 63

1 Answers1

1

You seem to be looking for combn.

t(combn(list, 2))
##      [,1]   [,2]     
## [1,] "john" "carl"   
## [2,] "john" "suzanne"
## [3,] "carl" "suzanne"
shadow
  • 21,823
  • 4
  • 63
  • 77