繁体   English   中英

如何在R中使用igraph计算仅具有输入和输出边缘的顶点?

[英]How to count vertices with only incoming and outgoing edges with igraph in R?

我正在尝试基于R中使用igraph包制作的图形计算仅具有入站或出站边的那些顶点。

这是我使用igraph在R中编写的代码:

library(igraph)
web <- read.csv(file = "myweb.csv", header = T) 
g=graph.data.frame(web) 

任何有关如何执行此操作的建议将不胜感激。

您可以使用degree()函数使用参数mode = 'in'mode='out'来确定顶点具有多少个入站或出站边。 以下是一些示例代码,用于计算具有传入和传出边缘的数量(以及哪些节点):

set.seed(123)
df <- data.frame(from = sample(1:20, 10), to = sample(1:20, 10))
library(igraph)
g <- graph.data.frame(df)
plot(g)

# Vertices with both incoming and outgoing links
V(g)[degree(g, mode = 'out')>0 & degree(g, mode = 'in') > 0]
#> + 3/17 vertices, named, from a36c786:
#> [1] 15 1  20
# number of vertices
length(V(g)[degree(g, mode = 'out')>0 & degree(g, mode = 'in') > 0])
#> [1] 3

# Number of vertices with outgoing links
length(V(g)[degree(g, mode = 'out')>0])
#> [1] 10

# Number of vertices with incoming links
length(V(g)[degree(g, mode = 'in')>0])
#> [1] 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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