【问题标题】:Find all intersecting vectors in a list of vectors in R在R中的向量列表中查找所有相交向量
【发布时间】:2019-09-29 16:09:18
【问题描述】:

我有一个向量列表sets,如下所示。

sets <- list(b = c("b4", "b5", "b6"),
             c = c("c2", "c3", "b4", "b5", "c6"),
             d = c("d1", "d2"),
             e = c("e45", "e55", "e65"),
             f = c("f4", "f5", "d1", "f6"),
             g = c("g1", "g2"),
             h = c("h5", "h6", "h7"),
             i = c("i9", "h5", "g1", "h6", "i8", "i7"),
             j = c("j1", "j2", "j3"))

我想识别此列表中所有唯一的元素,以及所有重叠/相交的元素。

如何在 R 中做到这一点?

unique <- list(e = c("e45", "e55", "e65"),
               j = c("j1", "j2", "j3"))

intersects <- list(d = c("d1", "d2"),
                   b = c("b4", "b5", "b6"),
                   c = c("c2", "c3", "b4", "b5", "c6"),
                   f = c("f4", "f5", "d1", "f6"),
                   g = c("g1", "g2"),
                   h = c("h5", "h6", "h7"),
                   i = c("i9", "h5", "g1", "h6", "i8", "i7"))

【问题讨论】:

  • 我无法理解您的问题。请更清楚。
  • 看起来每个字符串中的数字无关紧要。也许@Crops 可以澄清一下?
  • 应该将组件 d 列为唯一,因为组件 df 之间存在非空交集?
  • @JorisChau 是的。现在修好了。

标签: r graph set igraph


【解决方案1】:

相交值

对于相交值,R 中有一个内置函数可以完成工作。 intersect 正是这个例子:

intersect(c("b4", "b5", "b6"),c("c2", "c3", "b4", "b5", "c6"))
# [1] "b4" "b5"

但是,如果您想将其应用于多个值,则需要使用另一个名为 Reduce 的内置函数示例:

sets <- list(b = c("b4", "b5", "b6"),
         c = c("c2", "c3", "b4", "b5", "c6"),
         d = c("d1", "d2"),
         e = c("e45", "e55", "e65"),
         f = c("f4", "f5", "d1", "f6"),
         g = c("g1", "g2"),
         h = c("h5", "h6", "h7"),
         i = c("i9", "h5", "g1", "h6", "i8", "i7"),
         j = c("j1", "j2", "j3"))

Reduce(intersect,sets)

source

列表中的唯一值

您可以使用do.call 函数,在本例中为:

unique(do.call("c",sets))
# [1] "b4"  "b5"  "b6"  "c2"  "c3"  "c6" ....

希望对你有帮助

【讨论】:

    【解决方案2】:

    鉴于列表元素应根据以下内容进行分区:

    • 列出具有空交叉点的元素 w.r.t.所有其他列表组件,
    • 列出具有非空交集的元素 w.r.t.其他一些列表组件,

    在基础 R 中实现此目的的方法如下:

    ## find set components w/ empty intersections w/ all other components
    isUnique <- sapply(seq_along(sets), function(i) length(intersect(sets[[i]], unlist(sets[-i]))) < 1)
    
    ## empty intersect components
    sets[isUnique]
    #> $e
    #> [1] "e45" "e55" "e65"
    #> 
    #> $j
    #> [1] "j1" "j2" "j3"
    
    ## non-empty intersect components 
    sets[!isUnique]
    #> $b
    #> [1] "b4" "b5" "b6"
    #> 
    #> $c
    #> [1] "c2" "c3" "b4" "b5" "c6"
    #> 
    #> $d
    #> [1] "d1" "d2"
    #> 
    #> $f
    #> [1] "f4" "f5" "d1" "f6"
    #> 
    #> $g
    #> [1] "g1" "g2"
    #> 
    #> $h
    #> [1] "h5" "h6" "h7"
    #> 
    #> $i
    #> [1] "i9" "h5" "g1" "h6" "i8" "i7"
    

    【讨论】:

      【解决方案3】:

      这是我获取相交列表的尝试:

      sets <- list(b = c("b4", "b5", "b6"),
                   c = c("c2", "c3", "b4", "b5", "c6"),
                   d = c("d1", "d2"),
                   e = c("e45", "e55", "e65"),
                   f = c("f4", "f5", "d1", "f6"),
                   g = c("g1", "g2"),
                   h = c("h5", "h6", "h7"),
                   i = c("i9", "h5", "g1", "h6", "i8", "i7"),
                   j = c("j1", "j2", "j3"))
      
      set.names <- names(sets)
      names(set.names) <- set.names
      
      sets.intersect <- lapply(set.names, function(x) {
        res <- lapply(set.names, function(y) {
          if (x != y) {
            intersect(sets[[x]], sets[[y]])
          }
          else (
            character(0)
          )
        })
        Filter(function(x) length(x) > 0, res)
      })
      
      output.intersect <- lapply(sets.intersect, function(x) {
        res <- unlist(unname(x))
      })
      output.intersect <- Filter(function(x) !is.null(x), output.intersect)
      
      # RESULT
      dput(output.intersect)
      structure(
        list(
          b = c("b4", "b5"), 
          c = c("b4", "b5"), 
          d = "d1", 
          f = "d1", 
          g = "g1", 
          h = c("h5", "h6"), 
          i = c("g1", "h5", "h6")
        ), .Names = c("b", "c", "d", "f", "g", "h", "i")
       )
      

      我尝试在不使用 for 循环的情况下执行此操作,这需要一些使用列表和向量名称的技巧。

      【讨论】:

        猜你喜欢
        • 2015-03-31
        • 1970-01-01
        • 2015-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        相关资源
        最近更新 更多