I'm making a "pairwise" array in R. Given the vector combo, I'm finding every permutation of 4 elements. Thus, a 4-dimensional "pairwise" array. My current approach is making it as a simple list, using nested sapply functions, like so:
fourList <- sapply(X = combo, FUN = function(h) {
    hi <- which(combo == h) #get index of h
    sapply(X = combo[hi:n], FUN = function(i) {
      ii <- which(combo == i) #get index of i
      sapply(X = combo[ii:n], FUN = function(j) {
        ji <- which(combo == j) #get index of j
        sapply(X = combo[ji:n], FUN = function(k) {
          list(c(h,i,j,k))
        })
      })
    })
  })
I'd like to make some sort of progress indicator, so I can report to the user what percentage of the array has been built. Ideally, I'd just take numberCasesCompleted and divide that by totalCases = length(combo)^4 to get the fraction that is done. However, I can't seem to figure out an algorithm that takes in hi, ji, and ii, and outputs the value numberCasesCompleted. How can I calculate this?
In the 2D (x by y) case (e.g: sapply(X, function(x) {sapply(X[xi:n], function(y) {list(c(x,y))}}), this could be calculated by sum(n - (x-2:x), y-(x-1)), but generalizing that to 4 dimensions sounds rather difficult.