【发布时间】:2015-05-31 04:47:21
【问题描述】:
您好,我想将邻接列表(3 列)转换为邻接矩阵。在这个论坛中,我找到了多个关于如何将边列表转换为邻接矩阵的示例。我成功地为两列列表做到了这一点。 我已经尝试了所有可以在网上找到的解决方案,但似乎我错过了一小步。
我尝试了什么
我的变量是用户、国家、书籍
User<-c("maman","sophia","Antoine")
Country<-c("Canada","USA","Mexico")
books<-c("Coelho","Rimbaud","The flight")
dat<-data.frame(User, Country,books)
User | Country | books
maman | Canada | Coelho
sophia| USA | Rimbaud
Antoine| Mexico | The flight
第一次尝试
library(igraph)
m<-as.matrix(dat)
g<-graph.adjacency(m, mode="directed") ### If that worked I could have used
"get.adjacency"
第二次尝试
试图将数据转换为边缘列表,但由于存在三列而出现错误
el<-as.matrix(dat)
g=graph.edgelist(el,directed=TRUE) # turns
异常输出
maman sophia Antoine Canada USA Mexico Coelho Rimbaud The fligth
maman 1 0 1 0 0 0 0 1 0
sophia 0 0 0 0 1 0 1 0 1
Antoine 0 1 1 0 1 0 0 1 0
Canada 1 0 1 0 0 1 0 1 1
USA 0 0 0 1 0 0 0 0 1
Mexico 0 0 0 0 1 1 1 0 0
Coelho 0 0 1 1 0 1 0 1 0
Rimbaud 1 0 1 1 0 0 0 1 1
The fligth 0 1 0 0 1 1 0 0 1
我想查看所有顶点之间的交互。类似的东西:http://sna.stanford.edu/sna_R_labs/output/lab_1/1.3_Krackhardt_Friendship.pdf
任何帮助或指示将不胜感激!!!
【问题讨论】:
标签: r matrix igraph adjacency-list adjacency-matrix