通过R中另一列的成对组合来计算一列的唯一值 [英] Count unique values of a column by pairwise combinations of another column in R

查看:47
本文介绍了通过R中另一列的成对组合来计算一列的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据框:

   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  3    B
11  4    D
12  4    B

我想通过代码列的成对组合来获得 ID列的唯一值的计数:

I would like to get the count of unique values of the column "ID" by pairwise combinations of the column "Code":

  Code.Combinations Count.of.ID
1              A, B           2
2              A, C           2
3              A, D           1
4              B, C           3
5              B, D           3
6              C, D           2

我已经搜索了解决方案在线,到目前为止还无法达到预期的效果。
任何帮助将不胜感激。谢谢!

I have searched for solution(s) online, so far haven't been able to achieve the desired result. Any help would be appreciated. Thanks!

推荐答案

这是 data.table 的解决方法问题。使用 combn 函数选择所有可能的代码组合,然后为每个唯一的 CodeComb 计数ID:

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

这篇关于通过R中另一列的成对组合来计算一列的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆