0

I need to calculate local clustering coefficient for a huge bunch of egocentric networks with 1.5 depth. All the files are edgelists named after ego-s, and are stored in 'edgelist' folder. Here is my code to batch import it:

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))

Now I need to calculate transitivity only for the egos, which names are stored as a chr in 'datanames'.

It seems like I can't use this variable as a value for vids parameter, 1) neither directly, 2) nor after convertion to numeric, double and integer in this function

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

because in the first case it returns the following error:

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

and after convertion to numeric types I get:

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

How can I accomplish my task then?

  • Please include a reproducible example, see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Gabor Csardi Jan 17 '15 at 15:32
  • I wish I could, but it was ego-networks of real people from on-line social network and it would take me some time to generate some random examples. I've solved this issue and post the solution here. – semenoffalex Jan 29 '15 at 09:01

1 Answers1

0

Here is the solution, provided by my colleague 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)`