I would like to merge two data tables while keeping the original order of the data tables. I would also like to have a final column to say whether the two ids match each other. All of this while keeping the original order of the data.table.
Test data.table shows the issue I am having, when you merge the two data.tables, the order of rows will sometimes be different.
If(g1.label==g2.label) then(match=="T")
library(data.table)
set.seed(100)
dt <- data.table(g1=c("A", "B", "C", "D", "E", "F", "L", "O", "P", "J"),
g2=c("G", "D", "C", "H", "K", "J", "L", "U", "I", "R"),
value= rnorm(10))
ids <- data.table(labels=c("A", "B", "C", "D", "E", "F", "L", "O",
"P", "J", "G", "H", "K", "U", "I", "R"),
ids=c(1:16))
test <- merge(dt, ids, by.x="g1", by.y="labels")
test2 <- merge(dt, ids, by.x="g2", by.y="labels")
# Desired output with original order
g1 g2 value g1.label g2.label match
A G -0.50219235 1 11 F
B D 0.13153117 2 4 F
C C -0.07891709 3 3 T
D H 0.88678481 4 12 F
E K 0.86014084 5 13 F
F J 1.09086728 6 10 F
L L 1.42053190 7 7 T
O U 0.93415334 8 14 F
P I 0.22375495 9 15 F
J R -0.35749574 10 16 F