fork download
  1. m <- matrix(1:15, 3, 5)
  2. cn <- combn(1:ncol(m), 2)
  3. res <- apply(cn, 2, function(x) {
  4. m[, x[1]] * m[, x[2]]
  5. })
  6. colnames(res) <- paste(cn[1, ], cn[2, ], sep = "_")
  7. print(m)
  8. print(res)
  9.  
Success #stdin #stdout 0.21s 39328KB
stdin
Standard input is empty
stdout
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15
     1_2 1_3 1_4 1_5 2_3 2_4 2_5 3_4 3_5 4_5
[1,]   4   7  10  13  28  40  52  70  91 130
[2,]  10  16  22  28  40  55  70  88 112 154
[3,]  18  27  36  45  54  72  90 108 135 180