如何计算R中数据表中的唯一组合? [英] How to count unique combinations from a data table in R?
问题描述
我有一个包含三列的数据表。前两个是数据点的集合(可以是A,B或C的分类数据)。第三列是两个数据点的集合,因为它们属于一个集合。我需要获取可能组合的计数,并需要将 A& C与 C& A作为同一组的一部分,因为这些对的顺序无关紧要。
I have a data table with three columns. The first two are a collection of the data points (categorical data that can be either A, B, or C). The third column is a concatenation of the two data points as they belong to a set. I need to get the counts of the possible combinations and need to count "A & C" as part of the same group as "C & A" since the order of these pairs doesn't matter.
以前,我设置了一个列,将 CAT1& CAT2和另一个列为 CAT2& CAT1连接起来,希望在那里汇总,但是这些数字当然不会加起来然后适当地。
Previously, I set up a column that concatenated "CAT1 & CAT2" and then another as "CAT2 & CAT1" hoping to sum up there, but those numbers of course don't add up properly then.
library(data.table)
dt1 = data.table(CAT1 = c('a','b','c','b','a','c','c','b','a','c'),
CAT2 = c('a','b','c','a','b','c','a','b','c','a'))
dt1[,merged := paste(dt1$CAT1, dt1$CAT2, sep = ' & ')]
counts = data.table(table(dt1$merged))
输出表 counts为我提供了合并列的所有唯一性,但是我需要在翻转数据点的任何地方进行总结( A& C + C& A)。我知道可以手动完成此操作,但是我要手工完成的实际数据点太多。
The output table "counts" gives me all the uniques of the merged column, but I need to sum up anywhere the data points are flipped ("A & C" + "C & A"). I recognize that this could be done manually, but I have far too many actual data points to do by hand.
推荐答案
dt1[,paste(sort(c(CAT1,CAT2)),collapse=" & "),by=1:nrow(dt1)][,table(V1)]
这篇关于如何计算R中数据表中的唯一组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!