我有一些与一些出版物有关的数据表如下:
id
并希望将其转换为边缘列表以输入igraph以显示共同作者网络(例如,RA和DJ一起有两篇论文)。我总共有大约7000个ID。我对网络位没问题,但我无法弄清楚如何从数据中获取边缘列表。你能帮我吗?
答案 0 :(得分:0)
你在寻找这样的东西:
library(igraph)
PubID <- c("169759","174843","174843","174843","174843","174843","171051","171051","171051","171719","171719","171719","169759","173847","173847")
Author <- c("ZJ","RA","DJ","JP","GS","Tv","MC","JR","CW","PB","MD","FO","FO","RA","DJ")
dt <- data.frame(Author,PubID)
levs <- unique(unlist(dt, use.names = FALSE))
adj <- table(lapply(dt, factor, levs))
g1 <- graph_from_adjacency_matrix( adj )
plot(g1)
我根据您的数据创建了一个邻接矩阵。
答案 1 :(得分:0)
这是基于与tidyverse争论的一些数据的解决方案。作者之间的普通论文数量在n
中存储在边缘属性
PubID <- c("169759","174843","174843","174843","174843","174843","171051","171051","171051","171719","171719","171719","169759","173847","173847")
Author <- c("ZJ","RA","DJ","JP","GS","Tv","MC","JR","CW","PB","MD","FO","FO","RA","DJ")
dt <- data.frame(Author,PubID)
library(tidyverse)
library(igraph)
dt %>%
mutate(author_id = as.integer(Author)) -> dt
dt %>%
inner_join(dt, by = "PubID") %>%
filter(author_id.x < author_id.y) %>%
count(Author.x, Author.y) %>%
graph_from_data_frame(directed = FALSE) -> g1
plot(g1)
as_data_frame(g1, what = "edges")