0

I am trying to generate a data frame that contains all of the results of possible combinations. I'm using the function

combn(x,m)
x <- 17
m <- some range of the numbers between 2 and 16

in a loop where each iteration corresponds to a new value of m. Each iteration of the loop returns a vector of length choose(n,k) where n is equivalent to m and x is equivalent to k. I want to append each resulting vector as a column in a dataframe that contains all of the results, but this is not straightforward since the length of each vector varies. I have been able to accomplish this by first establishing a dataframe of NA values (data.frame) that is then incrementally filled by the values of the new.vector with the below loop:

n <- max(length(data.frame), length(new.vector))
 for(l in 0:n) {
 data.frame[l,j-1] <- new.vector[l] 
 }

I have two questions:

  1. Is there a better way to append a new column that differs in length from the previous columns in the data frame that uses the power of R and vector operations rather than doing this via a loop?
  2. Since this method works, I can go with it, but I've struggled to find the way to set the maximum number of rows in the dataframe that I initialize. It should be the maximum of choose(n,k1), choose(n,k2), choose(n,k3) ... choose(n,kn). I'm currently using the below to initialize the dataframe, but it generates the absolute maximum for a given n, which may be more rows than necessary depending on the range of k values.

    dataframe <- data.frame(matrix(NA, nrow = ncol(combn(n,length(n)/2)), 
    ncol = max.n-min.n+1))
    
amberwaves
  • 35
  • 7
  • For each value of `m`, `combn(x,m)` returns an array with `m` rows and `choose(x,m)` columns. I am not sure I understand which vector you want to append to your dataframe. – Lamia May 19 '17 at 22:47
  • Right- and I use those dimensions to generate my data frame. Each iteration of my loop generates a vector. The length of the vector of the first iteration is choose(x,2). For the second iteration, the length is choose(x,3). For the third iteration, the length is choose(x,4). But depending on what I want to look at, the length of the vector of the first iteration could be choose(x,5), then the length of the next would be choose(x,6), etc. So each of these vectors has differing lengths. And the maximum will be ncol(combn(n,length(n)/2)), but only if the value of n/2 is included in the range. – amberwaves May 19 '17 at 22:54
  • Could you describe how you obtain each vector? For example, for `m=2`, what does the vector correspond to? – Lamia May 19 '17 at 22:57
  • If you have a function `myfunction(m,x)` which returns a vector of length `choose(x,m)`, you can do `lapply(2:16, function(y) myfunction(y,x))`. This will return a list of vectors of unequal lengths. Then to convert to a data.frame with padded NAs, check this question https://stackoverflow.com/questions/7196450/create-a-data-frame-of-unequal-lengths – Lamia May 19 '17 at 23:25

0 Answers0