regex - Matching elements within list in R -
imagine have list, defined below:
test <- list(1:4,5,8,1,2:3,10)
i somehow match elements of list if contain similar numbers. example match test[[1]]
, test[[4]]
because both contain 1. likewise, test[[1]]
, test[[5]]
match because both contain 2 , 3.
after matching, construct unique id corresponds each match. example answer following list
ans <- list(1,2,3,1,1,4)
edit: the intuition behind answer if elements of list test
share common match, receive same id. means though test[[4]]
, test[[5]]
don't match, fact each match test[[1]]
means assigned same id.
this toy example. in practice apply matching large list (>100,000k elements). in mind algorithm need efficient.
thanks in advance!
here's 1 way using data.table
:
require(data.table) # generate data.table data id = rep(seq_along(test), sapply(test, length)) dt = data.table(id, val = unlist(test)) # solution dt[, ans := id[1l], by=val][, ans := .grp, by=ans][, ans[1l], by=id]$v1 # [1] 1 2 3 1 1 4