 <p>Here's a quick function:</p> <pre><code>myfun &lt;- function(df){ z &lt;- colSums(df) matrix(rowSums(expand.grid(z, z)), ncol = ncol(df)) } </code></pre> <p>It first takes the <code>colSums</code> as <code>z</code>. Then we use <code>expand.grid</code> to take all combinations of <code>z</code> to <code>z</code> and takes the <code>rowSums</code>. The output is coerced to a matrix with the correct number of columns.</p> <pre><code>myfun(df) [,1] [,2] [,3] [,4] [1,] 16 17 19 13 [2,] 17 18 20 14 [3,] 19 20 22 16 [4,] 13 14 16 10 </code></pre>