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?

4

1 回答 1

1

你似乎在寻找combn.

t(combn(list, 2))
##      [,1]   [,2]     
## [1,] "john" "carl"   
## [2,] "john" "suzanne"
## [3,] "carl" "suzanne"
于 2015-02-05T16:40:41.150 回答