【发布时间】:2016-04-14 09:34:17
【问题描述】:
我通过stackoverflow的大量信息搜索了很多以找到解决方案,但我被卡住了!我正在通过阅读和实践来学习 R 和 igraph,所以如果问题太简单,请多多包涵:)
我一直在使用下面的代码从谷歌学者个人资料页面中提取共同作者的文本数据(邻接列表),我想把它变成共同作者网络,但我没有成功在 Igraph 中使用 graph_from_adjlist ;它没有以正确的方式构建网络,所以我改变了我的方法并尝试先将它们变成边缘列表,然后使用更常见的 graph_from_edgelist 函数,我找到了一个解决方案here;当行数(在我的情况下是出版物)小于 300 时,它工作正常,但除此之外,它在 R 中给出了这个错误:
Error in rep(x[1], length(x) - 1) : invalid 'times' argument
Called from: FUN(X[[i]], ...)
Browse[1]> Q
老实说,我不知道将邻接列表的列转换为 2 列边缘列表的代码逻辑,我无法找出问题所在。
这是我的一小段代码(我已经描述了内联 cmets 中的每个步骤):
library(scholar)
library(igraph)
# one scholar profile link (works fine with small number of authors)
scurl <- "https://scholar.google.com/citations?user=nG42BMAAAAAJ&hl=en"
# prof Welman google scholar link as an example that gives the above error
# scurl <- "https://scholar.google.com/citations?user=_q2NODAAAAAJ&hl=en"
citid <- strsplit((strsplit(scurl,"&",fixed = TRUE)[[1]][1]),"=",fixed = TRUE)[[1]][2]
# authors <- as.data.frame(cSplit(subset(get_publications(citid,flush = TRUE),select = "author"),splitCols = "author",sep = ",")) ## this I put to check if authors are extracting in a right way
pub <- get_publications(citid,flush = TRUE)
coauthors <- as.character(tolower(pub$author)) ##to make text differences less effective in result
adjlist=strsplit(coauthors,",") # splits the character strings into list with different vector for each line
col1 <- unlist(lapply(adjlist,function(x) rep(x[1],length(x)-1))) # establish first column of edgelist by replicating the 1st element (=ID number) by the length of the line minus 1 (itself)
col2 <- unlist(lapply(adjlist,"[",-1)) # the second line I actually don't fully understand this command, but it takes the rest of the ID numbers in the character string and transposes it to list vertically
edgelist <- cbind(col1,col2) # creates the edgelist by combining column 1 and 2.
coauthorgraph <- graph_from_edgelist(edgelist,directed = FALSE)
set.seed(333)
coauthorgraph$layout <- layout.circle
tkplot(coauthorgraph)
我尝试将 (times=400) 条件添加到 col2 行,但没有帮助。 听到任何建议,我都会非常感激。
【问题讨论】: