R - 基于模块条件的度数计算
R - Degree calculation based module conditions
我有一个数据框,想计算
一个国家(节点)的度数-它的模数/它的模数的标准差。
我已经通过
计算了模块的度数和sd
dgclust <- aggregate(clust[, 2], list(clust$modularity_class), mean)
sdclust <- aggregate(clust[, 2], list(clust$modularity_class), sd)
但我不确定如何编写上面的计算,以便代码将 dgclust
和 sdclust
链接到国家所属的特定模块?我的预期输出是基于所述计算的每个国家/地区的值。任何帮助表示赞赏!
可重现的例子
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")
在 base R 中,我们可以使用 ave
为每个 modularity_class
计算 (Degree - mean)/sd
。
clust <- transform(clust, result = ave(Degree, modularity_class,
FUN = function(x) (x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE)))
这也可以写成dplyr
和data.table
-
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]
我有一个数据框,想计算 一个国家(节点)的度数-它的模数/它的模数的标准差。
我已经通过
计算了模块的度数和sddgclust <- aggregate(clust[, 2], list(clust$modularity_class), mean)
sdclust <- aggregate(clust[, 2], list(clust$modularity_class), sd)
但我不确定如何编写上面的计算,以便代码将 dgclust
和 sdclust
链接到国家所属的特定模块?我的预期输出是基于所述计算的每个国家/地区的值。任何帮助表示赞赏!
可重现的例子
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")
在 base R 中,我们可以使用 ave
为每个 modularity_class
计算 (Degree - mean)/sd
。
clust <- transform(clust, result = ave(Degree, modularity_class,
FUN = function(x) (x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE)))
这也可以写成dplyr
和data.table
-
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]