簡體   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