将逗号分隔的邻接列表转换为2列边缘列表(构建igraph对象)

时间:2016-01-09 20:17:26

标签: r igraph

我通过stackoverflow的大量信息搜索了很多信息以找到解决方案,但我被卡住了!我正在通过阅读和做法学习R和igraph,所以如果问题太简单,请耐心等待:)

我一直在使用以下代码从谷歌学者资料页面中提取共同作者身份的文本数据(邻接列表),我想把它变成共同作者网络,但我没有成功在Igraph中使用graph_from_adjlist ;它没有以正确的方式构建网络,所以我改变了我的方法并尝试先将它们转换为edgelist,然后使用更常见的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列边缘列表时代码的逻辑,我无法找出问题所在。

这是我的一小段代码(我已经描述了内联注释中的每一步):

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)

我尝试在col2行中添加(times = 400)条件,但没有帮助。  听到任何建议,我将非常感激。

1 个答案:

答案 0 :(得分:0)

一列是每个元素减去第一列,另一列是重复向量长度的第一个元素 - 1.你可以用import UIKit class ViewController: UIViewController { @IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! @IBOutlet weak var AgeSwitcherVar: UIStepper! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func AgeSwitcher(sender: UIStepper) { label2.text = String(Int(sender.value)) } @IBAction func SubmitButton(sender: AnyObject) { if label2.text == "18"{ label1.textColor = UIColor.purpleColor() label1.text = "congradulations you passed the test" } } } import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } 得到它。所以,在获得rep(..., times=lengths(adjlist)) - 1L之后接受

pub

enter image description here

更新

该错误来自于试图给出一个负## tolower does character conversion, and remove the trailing "..." coauthors <- sub('[ ,.]+$', '', tolower(pub$author)) ## Make edgelist by repeating 1st elements each length(vector)-1L adjlist <- strsplit(coauthors, '\\s*,\\s*') edgelist <- cbind( unlist(lapply(adjlist, tail, -1L)), # col1 rep(sapply(adjlist, `[`, 1L), times=lengths(adjlist)-1L) # col2 ) ## make graph g <- graph_from_edgelist(edgelist, directed=FALSE) ## Offset labels a bit: nodes printed from +x-axis counter-clockwise ord <- V(g) # node order theta <- seq(0, 2*pi-2*pi/length(ord), 2*pi/length(ord)) # angle theta[theta>pi] <- -(2*pi - theta[theta>pi]) # convert to [0, pi] dists <- rep(c(1, 0.7), length.out=length(ord)) # alternate distance ## Plot plot(g, layout=layout.circle, vertex.label.degree=-theta, vertex.label.dist=dists, vertex.label.cex=1.1, vertex.size=14, vertex.color='#FFFFCC', edge.color='#E25822') 参数(即,当列表中的某个元素不包含任何作者时)。

要添加自循环并删除新查询中出现的零字符条目,您需要首先过滤共同创作者列表以仅包含带有字符的条目,然后在adjlist中重复那些长度为== 1的元素。其余的应该是一样的。

times

然后像以前一样继续。