从igraph中的自我网络列表计算指定顶点id的传递性

时间:2015-01-16 10:54:06

标签: r igraph sna

我需要为1.5个深度的大量自我中心网络计算局部聚类系数。所有文件都是以ego-s命名的边缘列表,并存储在' edgelist'夹。这是批量导入它的代码:

datanames <- as.character(lapply(list.files("./edgelists"), FUN = function(x) strsplit(x, split="\\.")[[1]][1]))
dataset <- lapply(datanames, function(x) list(assign(x, read.table(paste("./edgelists/", x, ".csv", sep=""), header=TRUE, sep=","))))
graphs <- lapply(dataset, function(dataset) graph.data.frame(dataset, directed=F, vertices=NULL))

现在我只需要为自我计算传递性,这些名称在&#39; datanames&#39;中存储为chr。

似乎我不能将此变量用作vids参数的值,1)既不直接,2)也不能在此函数中转换为numeric,double和integer之后

trans <- lapply(graphs, function(graph) transitivity(graph, type = "local", vids=datanames))

因为在第一种情况下它返回以下错误:

1) Error in as.igraph.vs(graph, vids) : Invalid vertex names 

在转换为数字类型后,我得到:

2)  Error in .Call("R_igraph_transitivity_local_undirected", graph, vids,  : 
  At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id 

我如何完成任务呢?

1 个答案:

答案 0 :(得分:0)

以下是我的同事Benjamin Lind提供的解决方案:

all_files <- list.files("./edgelists") # reading file names
datanames <- strsplit(all_files, split = "\\.") # removing file extension
datanames <- sapply(datanames, "[[", 1) # getting names of egos

# Helper function to load data
fun1 <- function(x){
        pathname <- paste("./edgelists/", x, ".csv", sep="")
        xdf <- read.table(pathname, header = TRUE, sep=",")
        return(xdf)
}
dataset <- lapply(datanames, fun1)
# Converting data to graph objects
graphs <- lapply(dataset, graph.data.frame, directed = FALSE)
# Helper function to get vertices names of ego
egovidfun <- function(vname, vgraph){
        return(which(V(vgraph)$name == vname))
}
# Transitivity function for selected egos
newtransfun <- function(vid, vgraph){
        return(transitivity(vgraph, type = "local", vids = vid)[1])
}
# Getting vertices for egos
egovids <- egovidfun(datanames, graphs)
# Calculating transitivity for selected egos
trans <- newtransfun(egovids, graphs)`