Copying/setting 二分图中相邻顶点的顶点属性
Copying/setting vertex attribute to adjacent vertices in bipartite graph
我与研究人员及其附属机构建立了双向共同作者网络。这些机构有一个顶点属性表示他们的国家(在 V(sg)$country 中)。我想将此属性复制到连接到该节点的每个研究人员,以便 "a" 将具有 "China" 的 "country" 属性,而 "e" 将具有 "US" 在下面的示例中:
此外,一些研究人员在其整个职业生涯中已经转移到其他国家的机构,因此与多个机构有联系。在这些情况下,如果他们从最常在其附近的国家/地区获得属性将是理想的(例如:"c" 将获得 "China" 而不是 "France"。
我是编码新手,这是我的第一个问题,所以我提前为我的菜鸟方式道歉。
可重现的例子
library(igraph)
set.seed(1)
sg<-sample_bipartite(10, 5, p=.3)
countries<-c("US","France","China")
V(sg)[type=="TRUE"]$country<-sample(countries,replace=TRUE,5)
V(sg)[type=="FALSE"]$label <- c("a","b","c","d","e","f","g","h","i","j")
V(sg)[type=="TRUE"]$label <- V(sg)[type=="TRUE"]$country
V(sg)$color[1:10] <- rgb(0,1,0,.5)
V(sg)$color[11:15] <- rgb(1,0,0,.5)
plot(sg)
这是一种方法:
library(igraph)
set.seed(1)
sg<-sample_bipartite(10, 5, p=.3)
countries<-c("US","France","China")
V(sg)[type=="TRUE"]$country<-sample(countries,replace=TRUE,5)
V(sg)[type=="FALSE"]$label <- c("a","b","c","d","e","f","g","h","i","j")
V(sg)[type=="TRUE"]$label <- V(sg)[type=="TRUE"]$country
V(sg)$color[1:10] <- rgb(0,1,0,.5)
V(sg)$color[11:15] <- rgb(1,0,0,.5)
plot(sg)
idx <- V(sg)$type==0
res <- lapply(ego(sg, idx, order = 1), function(x)
names(tail(sort(table(V(sg)$country[x[-1]])), 1))
)
V(sg)$country[idx] <- res
unlist(V(sg)$country[V(sg)$label=="a"])
# [1] "China"
unlist(V(sg)$country[V(sg)$label=="e"])
# [1] "US"
unlist(V(sg)$country[V(sg)$label=="c"])
# [1] "China"
我与研究人员及其附属机构建立了双向共同作者网络。这些机构有一个顶点属性表示他们的国家(在 V(sg)$country 中)。我想将此属性复制到连接到该节点的每个研究人员,以便 "a" 将具有 "China" 的 "country" 属性,而 "e" 将具有 "US" 在下面的示例中:
此外,一些研究人员在其整个职业生涯中已经转移到其他国家的机构,因此与多个机构有联系。在这些情况下,如果他们从最常在其附近的国家/地区获得属性将是理想的(例如:"c" 将获得 "China" 而不是 "France"。
我是编码新手,这是我的第一个问题,所以我提前为我的菜鸟方式道歉。
可重现的例子
library(igraph)
set.seed(1)
sg<-sample_bipartite(10, 5, p=.3)
countries<-c("US","France","China")
V(sg)[type=="TRUE"]$country<-sample(countries,replace=TRUE,5)
V(sg)[type=="FALSE"]$label <- c("a","b","c","d","e","f","g","h","i","j")
V(sg)[type=="TRUE"]$label <- V(sg)[type=="TRUE"]$country
V(sg)$color[1:10] <- rgb(0,1,0,.5)
V(sg)$color[11:15] <- rgb(1,0,0,.5)
plot(sg)
这是一种方法:
library(igraph)
set.seed(1)
sg<-sample_bipartite(10, 5, p=.3)
countries<-c("US","France","China")
V(sg)[type=="TRUE"]$country<-sample(countries,replace=TRUE,5)
V(sg)[type=="FALSE"]$label <- c("a","b","c","d","e","f","g","h","i","j")
V(sg)[type=="TRUE"]$label <- V(sg)[type=="TRUE"]$country
V(sg)$color[1:10] <- rgb(0,1,0,.5)
V(sg)$color[11:15] <- rgb(1,0,0,.5)
plot(sg)
idx <- V(sg)$type==0
res <- lapply(ego(sg, idx, order = 1), function(x)
names(tail(sort(table(V(sg)$country[x[-1]])), 1))
)
V(sg)$country[idx] <- res
unlist(V(sg)$country[V(sg)$label=="a"])
# [1] "China"
unlist(V(sg)$country[V(sg)$label=="e"])
# [1] "US"
unlist(V(sg)$country[V(sg)$label=="c"])
# [1] "China"