For this very specific case I think following approach might be faster.
You create a list of data.tables with specific columns that you need to combine vertically (i.e. one
s and two
s). That is accomplished by lapply(seq(3,15,3), function(j) { DT[, c(1, j-1,j+1), with=F ]})
and you use rbindlist
which is a faster version of rbind
which joins data.tables
or data.frames
together without checking the column orders etc. (which is what makes it fast).
Finally you merge the resultant data.table DT2
back with part of original data.table DT
which you want repeated. i.e DT1
which has only columns names, two, twoa, twob, twoc, twod
from the original data.table
DT <- data.table(dt)
DT1 <- DT[, list(names, two, twoa, twob, twoc, twod)]
DT2 <- rbindlist(lapply(seq(3,15,3), function(j) { DT[, c(1, j-1,j+1), with=F ]}))
setkey(DT1, names)
setkey(DT2, names)
RES <- DT1[DT2]
RES
## names two twoa twob twoc twod one three
## 1: a 586 715 939 561 911 23 8
## 2: a 586 715 939 561 911 24 6
## 3: a 586 715 939 561 911 25 4
## 4: a 586 715 939 561 911 26 4
## 5: a 586 715 939 561 911 26 2
## 6: b 9 996 242 72 709 23 3
## 7: b 9 996 242 72 709 24 3
## 8: b 9 996 242 72 709 25 7
## 9: b 9 996 242 72 709 26 6
## 10: b 9 996 242 72 709 26 10
## 11: c 294 506 565 852 571 23 1
## 12: c 294 506 565 852 571 24 8
## 13: c 294 506 565 852 571 25 8
## 14: c 294 506 565 852 571 26 10
## 15: c 294 506 565 852 571 26 5
## 16: d 277 489 181 911 915 23 7
## 17: d 277 489 181 911 915 24 5
## 18: d 277 489 181 911 915 25 6
## 19: d 277 489 181 911 915 26 3
## 20: d 277 489 181 911 915 26 9
## 21: e 811 647 901 225 899 23 9
## 22: e 811 647 901 225 899 24 9
## 23: e 811 647 901 225 899 25 5
## 24: e 811 647 901 225 899 26 5
## 25: e 811 647 901 225 899 26 3
## 26: f 260 827 84 626 59 23 6
## 27: f 260 827 84 626 59 24 7
## 28: f 260 827 84 626 59 25 3
## 29: f 260 827 84 626 59 26 8
## 30: f 260 827 84 626 59 26 1
## 31: g 721 480 896 69 46 23 4
## 32: g 721 480 896 69 46 24 2
## 33: g 721 480 896 69 46 25 1
## 34: g 721 480 896 69 46 26 2
## 35: g 721 480 896 69 46 26 4
## 36: h 900 836 886 512 982 23 2
## 37: h 900 836 886 512 982 24 4
## 38: h 900 836 886 512 982 25 10
## 39: h 900 836 886 512 982 26 9
## 40: h 900 836 886 512 982 26 7
## 41: i 942 510 718 799 205 23 5
## 42: i 942 510 718 799 205 24 1
## 43: i 942 510 718 799 205 25 2
## 44: i 942 510 718 799 205 26 1
## 45: i 942 510 718 799 205 26 8
## 46: j 73 526 560 964 921 23 10
## 47: j 73 526 560 964 921 24 10
## 48: j 73 526 560 964 921 25 9
## 49: j 73 526 560 964 921 26 7
## 50: j 73 526 560 964 921 26 6
## names two twoa twob twoc twod one three
Another way is to just use data.table:::melt.data.table
function twice (once for ones
and another time for threes
and cbind
the results together
RES <- cbind(data.table:::melt.data.table(DT, id.vars=c(1,seq(3,15,3)),
measure.vars = seq(2,14,3), value.name='one', variable.name="onevar")[,-7, with=F],
data.table:::melt.data.table(DT, id.vars=c(1), measure.vars = seq(4,16,3),
value.name='three', variable.name="threevar")[, 3, with=F])
RES
## names two twoa twob twoc twod one three
## 1: a 586 715 939 561 911 23 8
## 2: a 586 715 939 561 911 24 6
## 3: a 586 715 939 561 911 25 4
## 4: a 586 715 939 561 911 26 4
## 5: a 586 715 939 561 911 26 2
## 6: b 9 996 242 72 709 23 3
## 7: b 9 996 242 72 709 24 3
## 8: b 9 996 242 72 709 25 7
## 9: b 9 996 242 72 709 26 6
## 10: b 9 996 242 72 709 26 10
## 11: c 294 506 565 852 571 23 1
## 12: c 294 506 565 852 571 24 8
## 13: c 294 506 565 852 571 25 8
## 14: c 294 506 565 852 571 26 10
## 15: c 294 506 565 852 571 26 5
## 16: d 277 489 181 911 915 23 7
## 17: d 277 489 181 911 915 24 5
## 18: d 277 489 181 911 915 25 6
## 19: d 277 489 181 911 915 26 3
## 20: d 277 489 181 911 915 26 9
## 21: e 811 647 901 225 899 23 9
## 22: e 811 647 901 225 899 24 9
## 23: e 811 647 901 225 899 25 5
## 24: e 811 647 901 225 899 26 5
## 25: e 811 647 901 225 899 26 3
## 26: f 260 827 84 626 59 23 6
## 27: f 260 827 84 626 59 24 7
## 28: f 260 827 84 626 59 25 3
## 29: f 260 827 84 626 59 26 8
## 30: f 260 827 84 626 59 26 1
## 31: g 721 480 896 69 46 23 4
## 32: g 721 480 896 69 46 24 2
## 33: g 721 480 896 69 46 25 1
## 34: g 721 480 896 69 46 26 2
## 35: g 721 480 896 69 46 26 4
## 36: h 900 836 886 512 982 23 2
## 37: h 900 836 886 512 982 24 4
## 38: h 900 836 886 512 982 25 10
## 39: h 900 836 886 512 982 26 9
## 40: h 900 836 886 512 982 26 7
## 41: i 942 510 718 799 205 23 5
## 42: i 942 510 718 799 205 24 1
## 43: i 942 510 718 799 205 25 2
## 44: i 942 510 718 799 205 26 1
## 45: i 942 510 718 799 205 26 8
## 46: j 73 526 560 964 921 23 10
## 47: j 73 526 560 964 921 24 10
## 48: j 73 526 560 964 921 25 9
## 49: j 73 526 560 964 921 26 7
## 50: j 73 526 560 964 921 26 6
## names two twoa twob twoc twod one three