R data.table generate random pairings in data table -
i have following sample data table.
id val 1: 1 2: b 3 3: c 2 4: d 1
i make random pairings amongst id
columns, not want id paired itself. efficient way data.tables? 1 approach have tried first find random rows in data table follows
x = x[sample(nrow(x),1),]
but hit block because have run check make sure current index not present in 1 returned. expensive computationally. example possible output result be
id val id.pair val.pair 1: 1 b 3 2: b 3 c 2 3: c 2 1 4: d 1 1
thanks in advance
you use combn
, sample.int
this:
df <- read.table(text="id val 1 b 3 c 2 d 1", header=true, stringsasfactors=false) library(data.table) dt <- data.table(df) set.seed(42) combis <- combn(dt[,id], 2)[,sample.int(choose(nrow(dt),2), nrow(dt))] setkey(dt, "id") cbind(dt[combis[1,],], dt[combis[2,],]) # id val id val # 1: c 2 d 1 # 2: b 3 d 1 # 3: 1 c 2 # 4: 1 d 1
however, if number of ids big need something function avoid calculating possible combinations.