【问题标题】:Count and identify unique relations in R计算和识别 R 中的唯一关系
【发布时间】:2016-10-01 06:21:45
【问题描述】:

我有一个包含 2 列的数据框,如下所示:

id1 <- c(123,456,789,122,345,678,901,126,567,890,001,002,130,122)
id2 <- c(121,122,123,456,125,126,127,678,129,130,131,132,890,987)
df <- cbind(id1,id2)
df
  id1 id2
 [1,] 123 121
 [2,] 456 122
 [3,] 789 123
 [4,] 122 456
 [5,] 345 125
 [6,] 678 126
 [7,] 901 127
 [8,] 126 678
 [9,] 567 129
[10,] 890 130
[11,]   1 131
[12,]   2 132
[13,] 130 890
[14,] 122 987

现在我可以计算 id1 和 id2 的组合等于 id2 和 id1 的组合的所有情况并返回它们,如下所示:

forwards<-paste(V1,V2)
backwards<-paste(V2,V1)

#identifying combinations

intersect(forwards, backwards)
[1] "456 122" "122 456" "678 126" "126 678" "890 130" "130 890"

#count combinations
length(intersect(forwards, backwards))
[1] 6

但是现在,对于 id1 仅与 id2 相关且 id2 仅与 id1 相关的所有情况,我想要一个新计数,例如对于 df,该计数将等于 4,因为:

id1==122 is related with id2==456 AND id1==456 is related with id2==122,
but id1 ==122 is too related with id2==987, 

因此,新计数应排除这两种情况,并按如下方式计数:

  id1 id2
  678 126
  126 678
  890 130
  130 890
  #count should be equals to 4

我该怎么做?

【问题讨论】:

    标签: r count


    【解决方案1】:

    这是我使用data.table 对您的问题的回答。也许有人可以帮助我们找到更直接的解决方案。

    library(data.table)
    df <- data.table(id1,id2) # get vectors as a data.table
    
    # create forwards and backwards  columns
      df[ , forwards := paste(id1,id2)]
      df[ , backwards := paste(id2,id1)]
    
    # count number of intersections between forwards and backwards  
      df [ forwards %in% backwards, .(count=.N)]
    
    >    count
    > 1:     6
    

    现在这就是您要问的,棘手的部分。

    # add new column with number of pairs of id1
      df[ , pairs :=.N, by= id1]
    
    # get all values that have more than one pair
      too_many_pairs <-  as.matrix(df[ pairs >1, .(id1,id2) ])
    
    # solution
      df[  id1 %in% id2 & id2 %in% id1 & !(id1 %in% too_many_pairs) ]
    
    >    id1 id2 
    > 1: 678 126 
    > 2: 126 678 
    > 3: 890 130 
    > 4: 130 890 
    

    解释解决方案:

    解决方案id1 %in% id2 &amp; id2 %in% id1第一部分表示只保留在 id2 中也可以找到的 id1 值,反之亦然

    解决方案!(id1 %in% too_many_pairs)第二部分表示删除所有超过一对的id1值

    【讨论】:

    • 好答案。加一。
    【解决方案2】:

    这是一个 Hadleyverse 方法:

    library(dplyr)
    library(tidyr)
    
    # make df a data.frame instead of a matrix
    data.frame(df) %>% 
        # add row index
        add_rownames() %>% 
        # melt to long form
        gather(id, val, -rowname) %>% 
        # filter down to values repeated an even number of times
        group_by(val) %>% filter(n() %% 2 == 0) %>% 
        # filter down to rows with two values
        group_by(rowname) %>% filter(n() == 2) %>% 
        # spread back to wide form
        spread(id, val)
    
    # Source: local data frame [4 x 3]
    # Groups: rowname [4]
    # 
    #   rowname   id1   id2
    #     (chr) (dbl) (dbl)
    # 1      10   890   130
    # 2      13   130   890
    # 3       6   678   126
    # 4       8   126   678
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 2020-03-19
      • 2021-07-20
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多