I have the following sample data table.
id val
1: a 1
2: b 3
3: c 2
4: d 1
I would like to make random pairings amongst the id
columns, however I do not want an id to be paired with itself. What would be the most efficient way to do this with data.tables? One approach I have tried is to first find random rows in a data table as follows
x = x[sample(nrow(x),1),]
but then I hit a block because I would have to run a check to make sure that current index is not present in the one returned. This would be expensive computationally. For example a possible output result would be
id val id.pair val.pair
1: a 1 b 3
2: b 3 c 2
3: c 2 a 1
4: d 1 a 1
Thanks in advance