I got this loop:
for(i in E(g)){
a = ends(g, i)[1]
b = ends(g, i)[2]
source_neighbors = neighbors(g, a)
target_neighbors = neighbors(g, b)
num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors))
print(num_overlap_neighbors)
}
g
is a gml graph, I was using igraph package. I want to rewrite it as a function, use sapply()
to apply the function to E(g)
to get a vector as output.
results=sapply(E(g), function(i){
a = ends(g, i)[1]
b = ends(g, i)[2]
source_neighbors = neighbors(g, a)
target_neighbors = neighbors(g, b)
num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors))
return(num_overlap_neighbors)
})
print(results)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.