自我网络中自我顶点的 igraph r 约束
igraph r constraint for ego vertices in ego networks
我喜欢计算自我网络中顶点的约束(例如,距离 1)。
我想使用应用函数自动执行此操作。
到目前为止,我可以计算自我网络所有顶点的约束。然而,我没有设法选择自我的约束,因为自我网络中自我顶点的位置不同(顶点是有序的,并且取决于属于自我网络的顶点,自我的位置不同)。
这是一个小样本网络,希望能阐明任务。
谢谢!
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,119,256,120,340,478,
622,116,330,231,116,5,330,451,231,231,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
我喜欢计算自我网络中顶点的约束(例如,距离 1)。 我想使用应用函数自动执行此操作。
到目前为止,我可以计算自我网络所有顶点的约束。然而,我没有设法选择自我的约束,因为自我网络中自我顶点的位置不同(顶点是有序的,并且取决于属于自我网络的顶点,自我的位置不同)。
这是一个小样本网络,希望能阐明任务。
谢谢!
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,119,256,120,340,478,
622,116,330,231,116,5,330,451,231,231,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