【问题标题】:Intersect all possible combinations of list elements与列表元素的所有可能组合相交
【发布时间】:2014-08-28 03:50:02
【问题描述】:

我有一个向量列表:

> l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven"))

> l
$A
[1] "one"   "two"   "three" "four"

$B
[1] "one" "two"

$C
[1] "two"  "four" "five" "six"

$D
[1] "six"   "seven"

我想计算列表元素的所有可能成对组合之间的重叠长度,即(结果的格式无关紧要):

AintB 2
AintC 2
AintD 0
BintC 1
BintD 0
CintD 1

我知道combn(x, 2) 可用于获取向量中所有可能的成对组合的矩阵,并且length(intersect(a, b)) 将给出两个向量的重叠长度,但我想不出一种方法把这两件事放在一起。

非常感谢任何帮助!谢谢。

【问题讨论】:

    标签: r list combinations intersect combn


    【解决方案1】:

    如果我理解正确,你可以看看crossprodstack

    crossprod(table(stack(l)))
    #    ind
    # ind A B C D
    #   A 4 2 2 0
    #   B 2 2 1 0
    #   C 2 1 4 1
    #   D 0 0 1 2
    

    如果你想要一个 data.frame 的相关值,你可以扩展这个想法,如下所示:

    1. 写一个漂亮的函数

      listIntersect <- function(inList) {
        X <- crossprod(table(stack(inList)))
        X[lower.tri(X)] <- NA
        diag(X) <- NA
        out <- na.omit(data.frame(as.table(X)))
        out[order(out$ind), ]
      }
      
    2. 应用它

      listIntersect(l)
      #    ind ind.1 Freq
      # 5    A     B    2
      # 9    A     C    2
      # 13   A     D    0
      # 10   B     C    1
      # 14   B     D    0
      # 15   C     D    1
      

    性能似乎相当不错。

    展开list

    L <- unlist(replicate(100, l, FALSE), recursive=FALSE)
    names(L) <- make.unique(names(L))
    

    设置一些功能进行测试:

    fun1 <- function(l) listIntersect(l)
    fun2 <- function(l) apply( combn( l , 2 ) , 2 , function(x) length( intersect( unlist( x[1]) , unlist(x[2]) ) ) )
    fun3 <- function(l) {
      m1 <- combn(names(l),2)
      val <- sapply(split(m1, col(m1)),function(x) {x1 <- l[[x[1]]]; x2 <- l[[x[2]]]; length(intersect(x1, x2))})
      Ind <- apply(m1,2,paste,collapse="int")
      data.frame(Ind, val, stringsAsFactors=F) 
    }
    

    查看时间:

    system.time(F1 <- fun1(L))
    #    user  system elapsed 
    #    0.33    0.00    0.33
    system.time(F2 <- fun2(L))
    #    user  system elapsed 
    #    4.32    0.00    4.31 
    system.time(F3 <- fun3(L))
    #    user  system elapsed 
    #    6.33    0.00    6.33 
    

    每个人似乎对结果的排序方式不同,但数字匹配:

    table(F1$Freq)
    # 
    #     0     1     2     4 
    # 20000 20000 29900  9900 
    table(F2)
    # F2
    #     0     1     2     4 
    # 20000 20000 29900  9900 
    table(F3$val)
    # 
    #     0     1     2     4 
    # 20000 20000 29900  9900 
    

    【讨论】:

    • 读者注意:stack 需要名称,如果您尝试将其与lists 一起使用。
    • 这是一个非常有效的解决方案!
    • 这太优雅了!!
    • 这很棒,为我节省了大量的循环交叉路口!
    【解决方案2】:

    combn 也适用于列表结构,您只需对结果进行一点unlist'ing 即可使用intersect...

    # Get the combinations of names of list elements
    nms <- combn( names(l) , 2 , FUN = paste0 , collapse = "" , simplify = FALSE )
    
    # Make the combinations of list elements
    ll <- combn( l , 2 , simplify = FALSE )
    
    # Intersect the list elements
    out <- lapply( ll , function(x) length( intersect( x[[1]] , x[[2]] ) ) )
    
    # Output with names
    setNames( out , nms )
    #$AB
    #[1] 2
    
    #$AC
    #[1] 2
    
    #$AD
    #[1] 0
    
    #$BC
    #[1] 1
    
    #$BD
    #[1] 0
    
    #$CD
    #[1] 1
    

    【讨论】:

    • 我如何获得与任何集合共有或相交的元素。在这里,正如您所解释并展示的那样,它显示了任何交叉点共有的元素总数
    【解决方案3】:

    试试:

    m1 <- combn(names(l),2)
    val <- sapply(split(m1, col(m1)),function(x) {x1 <- l[[x[1]]]; x2 <- l[[x[2]]]; length(intersect(x1, x2))})
    Ind <- apply(m1,2,paste,collapse="int")
    data.frame(Ind, val, stringsAsFactors=F)   
    #      Ind val
    # 1 AntB   2
    # 2 AntC   2
    # 3 AntD   0
    # 4 BntC   1
    # 5 BntD   0
    # 6 CntD   1
    

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多