I have a graph object (a weighted network), where nodes belong to different modularity classes defined in a dataframe. I would like to calculate a node's number of links to other nodes in all different modules (its own module and every other module).
How I can write this calculation based on my graph object g
and my dataframe modules
?. My expected output is a dataframe with each node's (country's) links to module 1, module 2, module 3, etc. Any help appreciated!
Reproducible example:
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")