r-通过图形和数据帧跨模块的节点链接



我有一个图对象(加权网络(,其中的节点属于数据帧中定义的不同模块化类。我想计算一个节点到所有不同模块(它自己的模块和其他模块(中其他节点的链接数。

如何根据我的图形对象g和数据帧modules编写此计算?。我期望的输出是一个数据帧,其中每个节点(国家/地区(都链接到模块1、模块2、模块3等。如有帮助,不胜感激!

可复制示例:

g <- structure(c(32, 12, 54, 0, 0, 0, 73, 0, 91, 0, 0, 65.27657092, 99, 
76, 0, 0, 0, 36.95395031, 0, 88, 44, 0, 0, 86.09277176, 0, 0, 0, 
84, 11, 0, 0, 0, 0, 0, 45, 0), .Dim = c(6L, 6L), .Dimnames = list(
c("Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", 
"Israel", "Italy"), c("Indonesia", "Iran..Islamic.Republic.of.", 
"Iraq", "Ireland", "Israel", "Italy")))                                                                                                                      
library(igraph)
g <- graph_from_adjacency_matrix(g)
modules <- structure(list(Label = structure(73:78, .Label = c("Afghanistan", 
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina", 
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", 
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", 
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada", 
"Central African Republic", "Chile", "China", "China, Hong Kong SAR", 
"China, Macao SAR", "China, Taiwan Province of", "Colombia", 
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia", 
"Democratic People's Republic of Korea", "Democratic Republic of the Congo", 
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France", 
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", 
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", 
"Hungary", "India", "Indonesia", "Iran (Islamic Republic of)", 
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", 
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", 
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania", 
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta", 
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco", 
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea", 
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", 
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation", 
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", 
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore", 
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", 
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania", 
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), class = "factor"), 
modularity_class = c(0L, 3L, 2L, 1L, 4L, 4L)), row.names = c("Indonesia", 
      "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy"
), class = "data.frame")

更新2

如果你想有重复边的无向图,你可以试试这个代码

g <- graph_from_adjacency_matrix(+(df > 0), diag = FALSE) %>%
as.undirected() %>%
set_vertex_attr(
name = "module",
value = with(modules, modularity_class[match(names(V(.)), Label)])
)
out <- sapply(V(g), function(x) {
m <- neighbors(g, x)$module
table(m[m != V(g)[x]$module])
})

使得

> out
$Indonesia
2 3
1 1
$Iran
0 1 2 4
1 1 1 1
$Iraq
0 1 3 4 
1 1 1 1
$Ireland
2 3 4
1 1 2
$Israel
1
1
$Italy
1 2 3
1 1 1

更新

如果您想查看分布,可以使用table

sapply(V(g), function(x) {
m <- neighbors(g, x)$module
table(m[m != V(g)[x]$module])
})

它给出

$Indonesia
2  3
99 73
$Iran
0  1  2
12 88 76
$Iraq
0  1  3
54 44 91
$Ireland
4
84
$Israel
< table of extent 0 >
$Italy
1  2  3
86 36 65

我们可以使用set_vertex_attrmodules中使用modularity_class设置顶点属性

g <- g %>%
set_vertex_attr(
name = "module",
value = with(modules, modularity_class[match(names(V(.)), Label)])
)

然后我们找到每个顶点的neighbors,并用具有不同模块性类的邻居的计数进行总结

out <- sapply(V(g), function(x) sum(neighbors(g, x)$module != V(g)[x]$module))[x]$module))

它给出

> out
Indonesia      Iran      Iraq   Ireland    Israel     Italy 
172       176       189        84         0       187

数据

df <- structure(c(
32, 12, 54, 0, 0, 0, 73, 0, 91, 0, 0, 65.27657092, 99,
76, 0, 0, 0, 36.95395031, 0, 88, 44, 0, 0, 86.09277176, 0, 0, 0,
84, 11, 0, 0, 0, 0, 0, 45, 0
), .Dim = c(6L, 6L), .Dimnames = list(
c(
"Indonesia", "Iran", "Iraq", "Ireland",
"Israel", "Italy"
), c(
"Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy"
)
))
modules <- structure(list(
Label = structure(73:78, .Label = c(
"Afghanistan",
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso",
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada",
"Central African Republic", "Chile", "China", "China, Hong Kong SAR",
"China, Macao SAR", "China, Taiwan Province of", "Colombia",
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia",
"Democratic People's Republic of Korea", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hungary", "India", "Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway",
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation",
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore",
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic",
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania",
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)",
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"
), class = "factor"),
modularity_class = c(0L, 3L, 2L, 1L, 4L, 4L)
), row.names = c(
"Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy"
), class = "data.frame")

最新更新