如何解决自我网络中自我顶点的 igraph r 约束
我喜欢计算自我网络中顶点的约束(例如,距离 1)。 我想使用应用函数自动执行此操作。
到目前为止,我可以计算自我网络所有顶点的约束。然而,我没有设法选择 ego 的约束,因为 ego 网络中 ego 顶点的位置是不同的(顶点是有序的,并且根据属于 ego 网络的顶点,ego 的位置会有所不同)。
这是一个小样本网络,希望能阐明任务。
谢谢!
library(igraph)
g <- graph.formula( Andre----Beverly:Diane:Fernando:Carol,Beverly--Andre:Diane:Garth:Ed,Carol----Andre:Diane:Fernando,Diane----Andre:Carol:Fernando:Garth:Ed:Beverly,Ed-------Beverly:Diane:Garth,Fernando-Carol:Andre:Diane:Garth:Heather,Garth----Ed:Beverly:Diane:Fernando:Heather,Heather--Fernando:Garth:Ike,Ike------Heather:Jane,Jane--Ike )
g <- simplify(g)
coords <- c(5,5,119,256,120,340,478,622,116,330,231,451,231)
coords <- matrix(coords,nc=2)
V(g)$label <- V(g)$name
g$layout <- coords
plot(g)
ego_1_graph <- make_ego_graph(
g,order = 1,nodes = V(g),mode = c("all"),mindist = 0
)
# ideally,I would want to calculate Ego's constraint in their ego network and store this value in a data frame with one line for each vertice.
dat <- data.frame(
Node_ID = names(V(g)),ego_1_cons = lapply(ego_1_graph,constraint) %>% unlist()
)
# constraint saves the constraint for each vertex in each ego network
# I need to access the individual ego networks (10 = one for each vertex)
# I need to pick the constraint only for the ego vertex
# Problem: vertices are ordered 1-10.
# Ego networks are constructed from all vertices with connections to the ego vertex or from connections among such vertices.
# Therefore,the ego vertex is not always the first vertex in the ego network.
#,E.g.:
# Vertex name: Heather (eighth vertex in the complete network)
cons8 <- round(constraint(ego_1_graph[[8]]),4)
cons8 # Heather's constraint is at position 3 of the list because of her connections to Fernando (fourth vertex in the complete network) and Garth (sixth vertex in the complete network)
cons8[[3]]
# How can I select the constraint value for the ego vertex?
# How Could I automate this for all ego networks in an apply function?```
解决方法
尝试sapply
,如下所示
dat <- transform(
data.frame(
Node_ID = names(V(g))
),ego_1_cons = sapply(
seq_along(Node_ID),function(k) constraint(ego_1_graph[[k]])[Node_ID[k]]
)
)
给出
> dat
Node_ID ego_1_cons
Andre Andre 0.7044271
Beverly Beverly 0.7044271
Diane Diane 0.4984568
Fernando Fernando 0.5541667
Carol Carol 0.9259259
Garth Garth 0.5541667
Ed Ed 0.9259259
Heather Heather 0.6111111
Ike Ike 0.5000000
Jane Jane 1.0000000
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。