0

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.

  • When I run your code I get the error `Error: object 'combinations' not found` - the object `combinations` is not created within the function, ditto with `powerset`. Are these objects created somewhere prior to calling `allSubsets()`? In order to help we need to make sure we can get the same output you're getting. – Stuart Allen Dec 10 '17 at 04:59
  • I just edited the post. I forgot to initialize combinations and powerset here. Let me know if you can help me now. – Mitul Shah Dec 10 '17 at 15:17

0 Answers0