Count unique values of a column by pairwise combinations of another column in R

前端 未结 4 1255
日久生厌
日久生厌 2020-12-07 03:51

Let\'s say I have the following data frame:

   ID Code
1   1    A
2   1    B
3   1    C
4   2    B
5   2    C
6   2    D
7   3    C
8   3    A
9   3    D
10          


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 04:27

    Here is a data.table way to solve the problem. Use combn function to pick up all possible combinations of Code and then count ID for each unique CodeComb:

    library(data.table)
    setDT(df)[, .(CodeComb = sapply(combn(Code, 2, simplify = F), 
                                    function(cmb) paste(sort(cmb), collapse = ", "))), .(ID)]
    # list all combinations of Code for each ID
             [, .(IdCount = .N), .(CodeComb)]    
    # count number of unique id for each code combination
    
    #    CodeComb IdCount
    # 1:     A, B       2
    # 2:     A, C       2
    # 3:     B, C       3
    # 4:     B, D       3
    # 5:     C, D       2
    # 6:     A, D       1
    

提交回复
热议问题