title: How to apply a bivariate function over the cross combination of two vectors to get a matrix in R?
body: <p>Say I have two vectors:</p>
  
  <pre><code>a &lt;- 1:4
  b &lt;- 1:2
  </code></pre>
  
  <p>and a bivariate function:</p>
  
  <pre><code>f &lt;- function(x,y) x**y
  </code></pre>
  
  <p>I would like to get a simple and efficient way (a one-liner?) to get (for this specific example):</p>
  
  <pre><code>[,1] [,2]
  [1,]    1    1
  [2,]    2    4
  [3,]    3    9
  [4,]    4   16
  </code></pre>
  
  <p>I can do:</p>
  
  <pre><code>res &lt;- matrix(nrow=length(a), ncol=length(b))
  for (i in 1:length(b)){
    res[,i] &lt;- mapply(f, a , b[i])
  }
  </code></pre>
  
  <p>but I want to avoid loops.</p>