Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并在公共R中甚至有一个元素的集合_R_Merge - Fatal编程技术网

合并在公共R中甚至有一个元素的集合

合并在公共R中甚至有一个元素的集合,r,merge,R,Merge,我有一个这样的列表: lista=list() lista[[1]]=c( 1, 2, 4, 6, 8, 9, 10, 11, 12, 19, 32, 34, 35, 36, 37, 38) lista[[2]]=c(7,8) lista[[3]]=c(13, 14, 16, 26, 27, 28, 29, 30, 31) lista[[4]]=c(20, 21, 23, 25, 27, 28, 29, 30) lista[[5]]=c(9,10

我有一个这样的列表:

lista=list()    
lista[[1]]=c( 1,  2,  4,  6,  8,  9, 10, 11, 12, 19, 32, 34, 35, 36, 37, 38)    
lista[[2]]=c(7,8)    
lista[[3]]=c(13, 14, 16, 26, 27, 28, 29, 30, 31)    
lista[[4]]=c(20, 21, 23, 25, 27, 28, 29, 30)    
lista[[5]]=c(9,10,39)
lista[[6]]=c(39,40)
group[[1]]=c(1,2,4,6,7,8,9,10,11,12,19,32,34,35,36,37,38,39,40)    
group[[2]]=c(13,14,16,20,21,23,24,26,27,28,29,30,31)
所以我希望我的输出如下:

lista=list()    
lista[[1]]=c( 1,  2,  4,  6,  8,  9, 10, 11, 12, 19, 32, 34, 35, 36, 37, 38)    
lista[[2]]=c(7,8)    
lista[[3]]=c(13, 14, 16, 26, 27, 28, 29, 30, 31)    
lista[[4]]=c(20, 21, 23, 25, 27, 28, 29, 30)    
lista[[5]]=c(9,10,39)
lista[[6]]=c(39,40)
group[[1]]=c(1,2,4,6,7,8,9,10,11,12,19,32,34,35,36,37,38,39,40)    
group[[2]]=c(13,14,16,20,21,23,24,26,27,28,29,30,31)
“打开盒子”:

lista=list()    
lista[[1]]=c( 1,  2,  4,  6,  8,  9, 10, 11, 12, 19, 32, 34, 35, 36, 37, 38)    
lista[[2]]=c(7,8)    
lista[[3]]=c(13, 14, 16, 26, 27, 28, 29, 30, 31)    
lista[[4]]=c(20, 21, 23, 25, 27, 28, 29, 30)    
lista[[5]]=c(9,10,39)
lista[[6]]=c(39,40)
group[[1]]=c(1,2,4,6,7,8,9,10,11,12,19,32,34,35,36,37,38,39,40)    
group[[2]]=c(13,14,16,20,21,23,24,26,27,28,29,30,31)
lista[[1]]
lista[[2]]
lista[[5]]
合并,因为它们有共同的元素

lista[[5]]
lista[[6]]
合并,因为它们有公共元素。 因此,
lista[[5]]
connect
lista[[1]]
lista[[2]]
lista[[5]]

我试着用这张票:


这里有一个可能的解决方案:

lista=list()    
lista[[1]]=c( 1,  2,  4,  6,  8,  9, 10, 11, 12, 19, 32, 34, 35, 36, 37, 38)    
lista[[2]]=c(7,8)    
lista[[3]]=c(13, 14, 16, 26, 27, 28, 29, 30, 31)    
lista[[4]]=c(20, 21, 23, 25, 27, 28, 29, 30)    
lista[[5]]=c(9,10,39)
lista[[6]]=c(39,40)


canBeMerged <- function(x,y){
  length(intersect(x,y)) > 0
}
mergeFun <- function(x,y){
  sort(union(x,y))
}

group <- Reduce(f=function(acc,curr){
  # since we wrapped each element inside a list with Map, here we unwrap the current element
  currVec <- curr[[1]]
  # we search in the accumulated list of "unmergeable" vectors 
  # if there is one which can be merged with currVec
  toMergeIdx <- Position(f=function(x) canBeMerged(x,currVec), acc)
  if(is.na(toMergeIdx )){ 
    # none can be merged, let's simply add currVec to the accumulated list
    acc[[length(acc)+1]] <- currVec
  }else{
    # currVec can be merged with the vector at position toMergeIdx, so we merge the 
    acc[[toMergeIdx]] <- mergeFun(acc[[toMergeIdx]],currVec)
  }
  return(acc)
},Map(lista,f=list))
说明:

Reduce
使用二进制函数依次组合给定向量的元素,例如给定元素向量
c(1,3,7)
和二进制函数
+
Reduce(c(1,3,7),f='+')
将调用函数一次执行
1+3
(Reduce的初始累积值是第一个值,如果没有指定),然后将再次调用函数,传递当前累积值
4
,并将其与下一个值求和,计算
4+7
,最后返回
11

因此,在本例中,我们希望使用Reduce对向量列表进行迭代,如果可以合并,则迭代组合它们;如果不能合并,则保留它们。 因此,在这种情况下,Reduce的累积值将是一个“不可合并”向量列表,将被检查并最终合并到下一个向量


请注意,由于Reduce的累积值和下一个值必须是同一类型的,因此我们需要使用
Map

lista
的每个元素包装在一个列表中,这里是另一种方法,它构造一个矩阵,显示列表中哪些元素彼此相交,并使用
igraph
包推导出组:

library(igraph)
## Construct the matrix
m = sapply(lista,function(x) sapply(lista,function(y) length(intersect(x,y))>0))
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
[1,]  TRUE  TRUE FALSE FALSE  TRUE FALSE
[2,]  TRUE  TRUE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE  TRUE  TRUE FALSE FALSE
[4,] FALSE FALSE  TRUE  TRUE FALSE FALSE
[5,]  TRUE FALSE FALSE FALSE  TRUE  TRUE
[6,] FALSE FALSE FALSE FALSE  TRUE  TRUE

## Determine the groups of the graph constructed from m
groups = groups(components(graph_from_adjacency_matrix(m)))
$`1`
[1] 1 2 5 6

$`2`
[1] 3 4

## Get the unique elements of each group
res = lapply(groups,function(x) sort(unique(unlist(lista[x]))))
$`1`
 [1]  1  2  4  6  7  8  9 10 11 12 19 32 34 35 36 37 38 39 40

$`2`
 [1] 13 14 16 20 21 23 25 26 27 28 29 30 31

你引用的帖子中给出的解决方案有什么问题吗?那篇帖子是对python的:)你能在代码中给出一些注释来开始我的研究吗?@ArthurCalegario:添加了一些代码注释和一个小解释谢谢你的注释尤达大师!我会研究的。现在还不太清楚。但是解决了我的问题:)在第一步中,sapply像double for一样工作,对吗?是的,嵌套的sapply像两个嵌套的for循环。谢谢你的帮助和关注拉米娅。了解aplly家庭:)