I have a data.table with two columns, one with a groupID and the other with a color. I want to find the length of the intersections or a pairwise intersection operation between all groups. There are similar posts online but nothing exactly as far as what I am looking for.
require(data.table)
set.seed(1)
x <- data.table(
groupID = paste0(sample(LETTERS), sample(LETTERS, replace = TRUE)),
color = sapply(1:length(LETTERS), function(x) sample(colors()[1:10])[1:sample(5:10)[1]])
)
x <- x[, .(color = unlist(color)), keyby = groupID]
The table below doesn't have correct values but it would look something like this:
groups <- x[, .N, keyby = groupID][,groupID]; results <- CJ(groups, groups)
results[, intersectionLength := sapply(1:nrow(results), function(x) sample(5:10)[1])]
EDIT
This post has a similar question. How could I apply this to my problem?
Here is one optioin with
Mapto compare the pairwise elements of group columns to extract theintersecting 'color' values and get thelengthof it