I am looking for a data.table way to potentially eliminate a double for loop. I am trying to compare every group with every other group with the DTW algorithm.
Here a MWE (not optimized):
library(data.table)
library(SimilarityMeasures)
tt <- data.table(A=1:100,B=2:101,C=rep(1:4,each=25))
result <- list()
for(i in 1:4){
for(j in 1:4){
result[[4*i+j]] <- DTW(tt[C==i,cbind(A,B)],tt[C==j,cbind(A,B)])
}
}
result
A and B are some variables that are used as the input to the DTW algorithm, C is the grouping variable. I tried to come up with some lapply solution combined with a .SD or .BY approach in data.table, but failed. The goal is to have a much faster function than the above double for loop (even when running it in parallel).
Any help is much appreciated. Thanks!