我有一个包含几个向量的列表,例如:
ls=list(c("g1","g3","g6"),c("g1","g4"),c("g2","g5"),c("g2","g5"),c("g2"))
我想捕获最少数量的元素,以便每个向量至少有一个元素。
所以在这个例子中," g1"和" g2"因为g1捕获向量1和2,g2捕获向量1,3,4和5。
我一直在关注How to find common elements from multiple vectors?,但问题并不完全相同。
答案 0 :(得分:1)
蛮力:
ls <- list(c("g1","g3","g6"),c("g1","g4"),c("g2","g5"),c("g2","g5"),c("g2"))
#unique values:
vals <- unique(do.call(c,ls))
#matrix indicating in which list each value is present
valsin <- sapply(ls,function(x) as.integer(vals %in% x))
rownames(valsin) <- vals
#loop through numbers of values to take for combinations
for (i in seq_along(vals)) {
cat(paste0(i,"\n"))
#Do combinations fullfill condition?
suff <- combn(seq_along(vals),i,FUN=function(x) {
identical(colSums(valsin[x,,drop=FALSE]),rep(1,length(ls)))
})
if (sum(suff) > 0) {
#combinations that fullfill condition
res <- combn(vals,i)[,suff]
#stop loop if condition has been fullfilled
break
}
}
res
#[1] "g1" "g2"