简体   繁体   中英

calculate Weighted In-Degree, Weighted Out-Degree of a node igraph in R

I have a graph generated by following code:

library(igraph)

dat <- data.frame(
  V0 = c(0L, 100L, 200L, 0L, 0L, 0L, 0L),
  V2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L),
  V3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L),
  V4 = c(120L, 0L, 0L, 0L, 0L, 0L, 0L),
  V6 = c(0L, 0L, 0L, 30L, 0L, 0L, 0L),
  V10 = c(180L, 0L, 0L, 90L, 0L, 0L, 0L),
  V12 = c(0L, 0L, 0L, 0L, 30L, 270L, 0L))
rownames(dat) <- c("V0","V2","V3","V4","V6","V10","V12")
dat <- data.matrix(dat)

g2 <- graph_from_adjacency_matrix(dat, weighted=TRUE)
plot(g2, vertex.size = 20, edge.label = E(g2)$weight)

The graph should look like this:在这个链接上查看

My expected output is to calculate the Weighted In-Degree, Weighted Out-Degree. Thank you a lot

You're looking for igraph::strength with mode = "in" and mode = "out" .

strength(g2, mode = "in")
# V0  V2  V3  V4  V6 V10 V12 
#300   0   0 120  30 270 300 

strength(g2, mode = "out")
# V0  V2  V3  V4  V6 V10 V12 
#300 100 200 120  30 270   0 

See also the manual .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM