[英]Add a node and delete an specified edge in R igraph
我正在R中學習igraph,並且對如何向圖中添加節點以及如何通過指定其兩個節點來刪除邊有疑問。 這是我的代碼,用於創建具有4個節點和一些邊緣的圖形。
g <- as.data.frame(cbind(matrix(c(1, 2, 1, 3, 1, 4, 3, 4, 2, 4), byrow = TRUE, ncol = 2), c(5.6, 2.7, 3.5, 1.8, 2.1)))
names(g) <- c("start", "end", "length")
g <- graph.data.frame(g, directed = FALSE)
plot(g)
這里的length
表示屬性edge(邊緣的長度)。 我想在圖上添加一個節點5。 該節點將在1和2之間。現在邊1--2的長度為5.6。 節點5與節點1之間的距離為2.6,與節點2之間的距離為3.0。我想添加這兩個邊(1--5和2--5)。 我還需要通過告訴R刪除節點1和節點2之間的邊來刪除1--2邊。
這樣做的簡單方法是什么? 謝謝。
就像是:
library(igraph)
g <- as.data.frame(cbind(matrix(c(1, 2, 1, 3, 1, 4, 3, 4, 2, 4),
byrow = TRUE, ncol = 2),
c(5.6, 2.7, 3.5, 1.8, 2.1)))
names(g) <- c("start", "end", "length")
g <- graph.data.frame(g, directed = FALSE)
# add node 5
g <- g + vertices(5)
# delete edge 1-2
g["1", "2"] <- NULL
# add new edges with length attribute
g <- g + edge("1", "5", attr=list(length=2.6))
g <- g + edge("2", "5", attr=list(length=1.3))
plot(g)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.