[英]Adjacency matrix from dataframe with igraph R
I am new at R and graphs, and I am trying to practice with a social signed network using the library igraph. 我是R和graphs的新手,我正在尝试使用igraph库在社交签名网络中进行练习。
I have a dataframe (df) which contains three columns. 我有一个包含三列的数据框(df)。 The first one is the voter, the second one the user who receives the vote, and the third one is the vote (-1 or 1 depending of the negative or positive vote, respectively).
第一个是投票者,第二个是接收投票的用户,第三个是投票(-1或1分别取决于否决票或赞成票)。
> head(df)
voter user vote
1 ludraman cjcurrie 1
2 blankfaze olivo -1
3 gzornenplatz cjcurrie 1
4 orthogonal olvion 1
5 andrevan cerviz 1
6 texture cjcurrie 1
I want to create a graph with igraph but firstly I need to obtain the adjacency matrix from df. 我想用igraph创建一个图,但首先我需要从df获取邻接矩阵。
I tried with the library sharpshootR 我尝试了图书馆SharpshootR
A <- component.adj.matrix(df[, c(1,2)], mu=df[, 1], co=df[, 2], wt=df[, 3])
Is there a simple way to obtain that adjacency matrix using the library igraph? 有没有一种简单的方法可以使用库igraph获得该邻接矩阵?
Thanks. 谢谢。
If I got your problem right, you can use graph_from_data_frame
from igraph
itself: 如果我的问题正确,则可以使用
igraph
本身的graph_from_data_frame
:
Data 数据
d <- structure(list(voter = c("ludraman", "blankfaze", "gzornenplatz", "orthogonal",
"andrevan", "texture"),
user = c("cjcurrie", "olivo", "cjcurrie", "olvion", "cerviz", "cjcurrie"),
vote = c(1L, -1L, 1L, 1L, 1L, 1L)),
row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")
igraph IGRAPH
library(igraph)
g <- graph_from_data_frame(d)
plot(g)
You can probably work from tehre (given your full data) to use other parts of the data in the visualization (like the score). 您可能可以使用tehre(给出完整的数据)来使用可视化中数据的其他部分(例如得分)。
This solution works for my problem: 此解决方案适用于我的问题:
edge_list <- training_edges[df]. # create a edge list
G <- graph.data.frame(edge_list, directed=TRUE) # create the graph
A <- as_adjacency_matrix(G,type="both",names=TRUE,
sparse=FALSE, attr = "vote") # create the adjacency matrix
Where A is the adjacency matrix. 其中A是邻接矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.