I would like to merge the data.table to itself based on values where id's are not equal. Here is a small example:
library(data.table)
#Two tables:
dt_1 <- data.table(id = c(1,2,3),x = c(2,3,4))
dt_2 <- copy(dt_1) %>%
setnames(c("id_new","x_new"))
#Calculation:
dt_2 <- dt_1[,as.list(dt_2),by = c("id")]
dt_2 <- merge(dt_2,dt_1,by = c("id"),all.x = TRUE)
dt_2[id!=id_new]
Maybe somebody can come up with a better solution.
A few options below with benchmarking. The best seems to be subsetting the 2-permutations of row indices where
idis different (f1).