title: In R, how do I get all possible combinations of the values of some vectors?
body: <p>The background: I have a function which takes some parameters. I want to have the result of the function for all possible parameter combinations. </p>
  
  <p>A simplified example: </p>
  
  <pre><code>f &lt;- function(x, y) { return paste(x, y, sep=",")} 
  colors = c("red", "green", "blue") 
  days = c("Monday", "Tuesday") 
  </code></pre>
  
  <p>I want my result to look like </p>
  
  <pre><code>     color    day         f    
  [1,] "red"    "Monday"    "red,Monday" 
  [2,] "red"    "Tuesday"   "red,Tuesday"
  [3,] "green"  "Monday"    "green,Monday"
  [4,] "green"  "Tuesday"   "green,Tuesday"
  [5,] "blue"   "Monday"    "blue,Monday"
  [6,] "blue"   "Tuesday"   "blue,Tuesday"
  </code></pre>
  
  <p>My idea is to create a matrix with the columns <code>color</code> and <code>day</code>, fill it using the existing vectors <code>colors</code> and <code>days</code>, initialize an empty column for the results, then use a loop to call f once per matrix row and write the result into the last column. But I don't know how to easily generate the matrix from the <code>colors</code> and <code>days</code> vector. I tried searching for it, but all results I got were for the <code>combn</code> function, which does something different. </p>
  
  <p>In this simplified case, the <code>colors</code> and <code>days</code> are factors, but in my real example, this is not the case. Some of the parameters to the function are integers, so my real vector may look more like <code>1, 2, 3</code> and the function will require that it is passed to it as numeric. So please no solutions which rely on factor levels, if they can't somehow be used to work with integers. </p>