0

我需要计算大量深度为 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))

现在我只需要计算 ego 的传递性,这些名称作为 chr 存储在“datanames”中。

似乎我不能将此变量用作 vids 参数的值,1)既不能直接使用,2)也不能在此函数中转换为数字、双精度和整数之后

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 

那我怎样才能完成我的任务呢?

4

1 回答 1

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)`
于 2015-01-29T09:18:10.583 回答