R: sna (social network analysis) package: data frame to adjacency matrix

StackOverflow https://stackoverflow.com/questions/16280221

  •  13-04-2022
  •  | 
  •  

Вопрос

I'm hoping for some more transformation help. Say I have a data frame:

df = data.frame(source = c("a","a","b"), 
                target = c("b","c","d"), 
                weight = c(1,1,1))

where the row: source = i | target = j | weight = w denotes a directed edge of weight w from node i to node j. Given an arbitrary data frame of this form, I am looking for a procedure to investigate the components of the induced graph G. Having read a comment by Ben on this question by me:

R: Gephi: manipulating dataframe to use with write.gexf

I checked out the package sna which seemed to have what I was looking for in the form of the function component.dist. So my question is 'how can I transform my data frame into a suitable form for the dat argument of the component.dist function (or is there a more 'optimum' way of approaching this problem?)' I tried the following code:

xtabs(weight ~ source + target, data = df)

but this has obvious shortfalls (e.g. it doesn't form an nrow(df) by nrow(df) matrix).

Thanks for any help.

Это было полезно?

Решение

I didn't find this result when first searching the internet regarding the above question, however, I have just come across it now:

How to calculate adjacency matrices in R

From first inspection, looks like it answers my question.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top