rmoduledegrees

R - Degree calculation based module conditions


I have a dataframe where would like to calculate the degree of a country (node) - degree of its module / standard deviation of its module.

I have calculated degree and sd of modules by

dgclust <- aggregate(clust[, 2], list(clust$modularity_class), mean)
sdclust <- aggregate(clust[, 2], list(clust$modularity_class), sd)

But I'm not sure how I can write the calculation above so that the code links dgclust and sdclust to the specific module the country belongs to? My expected output is a value for each country based on the described calculation. Any help appreciated!

Reproducible example

clust <- dput(head(clust[1:10, c(1, 5,6)]))
structure(list(Label = structure(1:6, .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"), 
    Degree = c(5L, 14L, 14L, 8L, 1L, 119L), modularity_class = c(3L, 
    4L, 2L, 2L, 1L, 2L)), row.names = c("Afghanistan", "Albania", 
"Algeria", "Angola", "Antigua and Barbuda", "Argentina"), class = "data.frame")

Solution

  • In base R, we can use ave to calculate (Degree - mean)/sd for each modularity_class.

    clust <- transform(clust, result = ave(Degree, modularity_class, 
              FUN = function(x) (x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE)))
    

    This can be written in dplyr and data.table as well -

    library(dplyr)
    
    clust <- clust %>%
      group_by(modularity_class) %>%
      mutate(result = (Degree - mean(Degree, na.rm = TRUE))/sd(Degree, na.rm = TRUE))
    
    
    library(data.table)
    setDT(clust)[, result := (Degree - mean(Degree, na.rm = TRUE))/sd(Degree, na.rm = TRUE), modularity_class]