title: How do you select multiple variables from a matrix using a randomly selected vector of column indices?
body: <p>Hopefully this has an easy answer I just haven't been able to find:</p>
  
  <p>I am trying to write a simulation that will compare a number of statistical procedures on different subsets of rows (subjects) and columns (variables) of a large matrix.</p>
  
  <p>Subsets of rows was fairly easy using a <code>sample()</code> of the subject ID numbers, but I am running into a little more trouble with columns.</p>
  
  <p>Essentially, what I'd like to be able to do is create a random sample of column index numbers which will then be used to create a new matrix. What's got me the closest so far is:</p>
  
  <pre><code>testmat &lt;- matrix(rnorm(10000),nrow=1000,ncol=100)
  column.ind &lt;- sample(3:100,20)
  teststr &lt;- paste("testmat[,",column.ind,"]",sep="",collapse=",")
  </code></pre>
  
  <p>which gives me a string that has a <code>testmat[,column.ind]</code> for every sampled index number. Is there any way to easily plug that into a <code>cbind()</code> function to make a new matrix? Is there any other obvious way I'm missing?</p>
  
  <p>I've been able to do it using a loop (i.e. <code>cbind(matrix,newcolumn)</code> over and over), but that's fairly slow as the matrix I'm using is quite large and I will be doing this many times. I'm hoping there's a couple-line solution that's more elegant and quicker.</p>