Consider the following data table
data.table(col1 = rep(LETTERS[1:3],3), col2 = rep(LETTERS[7:9],3), col3 = 1:9)
col1 col2 col3
1: A G 1
2: B H 2
3: C I 3
4: A G 4
5: B H 5
6: C I 6
7: A G 7
8: B H 8
9: C I 9
Here col1 and col2 are categorical variables. Now do do I group the data such as below
col1 col2 col3
1: A G [1, 4, 7]
2: B H [2, 5, 8]
3: C I [3, 6, 9]
Basically here I can use group by
to get combinations but I guess it wont give me the col3 as required. It can be done by writing two for
loops, but consider a situation when there are higher number of these kind of variables. Can this be done without going for the for
loops.