例如,如果我们有一个1-2-3图并删除了顶点2,则该图将是1-3。我有一个带有10000000+个顶点的巨大图形,所以我无法手动删除和创建所有这些顶点。当我使用delete.vertices(g, verticesToDelete)
时,它会自动删除其与邻居的边缘。
假设我们有一个stackoverflow用户和徽章的图形,其中边缘表示用户拥有该徽章。我想在所有拥有该徽章的用户之间保持优势。下面是一个代码示例:
users <- c(1,2,3,4,5,6,7,8)
badges <- c('Teacher','Teacher','Teacher','Student','Student','Student','Popular Question','Popular Question')
edgeList <- data.frame(users,badges)
library(igraph)
g <- graph_from_data_frame(edgeList,directed = FALSE)
plot(g)
verticesToDelete <- c('Teacher','Student','Popular Question')
g2 <- delete.vertices(g, verticesToDelete)
plot(g2)
# I want the graph to be like the one below after the deletions
users1 <- c(1,1,2,4,4,5,7)
users2 <- c(2,3,3,5,6,6,8)
edgeList2 <- data.frame(users1,users2)
g3 <- graph_from_data_frame(edgeList2,directed = FALSE)
plot(g3)
答案 0 :(得分:0)
怎么样?
edgeList <- data.frame(users,badges)
edgeList_badges <- merge(edgeList,edgeList,by="badges",
all=T)
edgeList_badges$badges <- NULL
edgeList_badges <-edgeList_badges %>% filter(users.x!=users.y)
edgeList_badges<-edgeList_badges[!duplicated(t(apply(edgeList_badges[1:2], 1, sort))), ]
g4 <- graph_from_data_frame(edgeList_badges,directed = FALSE)
plot(g4)
edgeList
与其自身合并,以获得具有相同徽章的所有用户组合这是另一种选择
library(DescTools)
edgeList <- data.frame(users,badges)
combSetTmp <- list()
for(badge in 1:length(verticesToDelete)){
tmp <- edgeList %>% filter(badges==verticesToDelete[badge]) %>% select(users)
combSetTmp[[badge]] <- CombSet(tmp$users,2)
}
combSet <- do.call(rbind, combSetTmp)
g4 <- graph_from_edgelist(combSet,directed = FALSE)
plot(g4)
应该更“便于记忆”