This is a network matrix:
x <- matrix(c(0,0,0,0,0,
0,0,1,0,0,
1,0,0,1,0,
0,1,0,0,1,
0,1,0,0,0), nrow = 5, byrow = TRUE)
This function will return all possible subnetworks of a network in a list (using the sna
library):
combinations <- list()
powerset <- list()
allSubsets <- function(networkMatrix) {
allNodes <- seq(1:nrow(networkMatrix))
for(i in 2:nrow(networkMatrix)) {
combinations[[i-1]] <- combn(allNodes, i)
}
for(i in 1:length(combinations)) {
for(j in 1:ncol(combinations[[i]])) {
if(connectedness(networkMatrix[combinations[[i]][,j],
combinations[[i]][,j]]) == 1 ) {
emptyMatrix <- matrix(data = NA, nrow = nrow(networkMatrix), ncol =
ncol(networkMatrix))
emptyMatrix[combinations[[i]][,j], combinations[[i]][,j]] <-
networkMatrix[combinations[[i]][,j], combinations[[i]][,j]]
emptyMatrix[is.na(emptyMatrix)] <- 0
powerset <- list(powerset, emptyMatrix)
}
}
}
return(powerset)
}
Print output of the function:
allSubsets(x)
I want this output in a proper formatted list so that I can extract any element from the list. For example allSubsets(x)[[1]]
, allSubsets(x)[[2]]
, etc.