title: Generating permutations of a 10-element vector with 8 zeros and 2 ones in R
body: <p>I'm generating 10-element vectors of 0s and 1s and varying the possible number of 1s from 2 to 9.  </p>
  
  <p>For example
  2 values = 1</p>
  
  <pre><code>1000000010
  </code></pre>
  
  <p>3 values = 1</p>
  
  <pre><code>1010100000
  </code></pre>
  
  <p><strong>EDIT: I want to efficiently generate all possible combinations.</strong></p>
  
  <p>I have a crude way of doing this but I'm guessing there is a simpler way to achieve this using some sort of permutation function.</p>
  
  <p>My way:
  Using the <code>sample</code> function in R:  I start with a vector of all zeros and draw 2 random numbers from 1 to 10 without replacement and uses those as indices of elements in the vector to change to 1.  I do this a lot of times then toss our the duplicates.</p>
  
  <pre><code>mat &lt;- matrix(data = 0,nrow = 1000,ncol = 10) #matrix to hold output
  vect &lt;- rep(NA, 1000) 
  v0 &lt;- rep(0,10)                              #initial vector of all 0s
  for(i in 1:dim(mat)[1]){
    mat[i, sample(1:10, 2)] &lt;- 1
  
    vect[i] &lt;- paste(mat[i,], collapse = "")   #collapse vector to a string
  }
  
  mat[duplicated(vect) !=T,]                       #remove duplicates
  </code></pre>
  
  <p>This is a round about way to do it but gets the job done.</p>