简体   繁体   English

EXCEL:计算唯一值的组合

[英]EXCEL: count combinations for unique values

I'm trying to count the following in excel: I have a number of unique values for X (say 4), each appearing twice (in random order) in the data set. 我试图在excel中计算以下各项:我有多个X的唯一值(例如4),每个值在数据集中出现两次(以随机顺序)。 I want to count the number of each combination (regardless of order) of values on a second column Y for each value of X. 我想计算X的每个值在第二列Y上的每个值组合(不考虑顺序)的数量。

Example (here in order): 示例(按顺序排列):

X Y

1 A
1 D
2 A
2 C
3 B
3 C
4 A
4 D

As output, I need: 作为输出,我需要:

n A,D 2
n A,C 1
n B,C 1
n B,D 0

(Let's assume all other combinations don't exist.) (假设所有其他组合都不存在。)

Is this possible without rearranging the data? 是否可以在不重新排列数据的情况下进行? I don't mind putting in 4 formulas (for each possible combination). 我不介意输入4个公式(每种可能的组合)。 My starting point was akin to this: excel count unique combinations of columns , but I can't quite figure it out and also I know what feels like next to nothing about excel or coding... 我的出发点与此类似: excel对列的唯一组合进行计数 ,但是我无法完全弄清楚,而且我知道对excel或编码几乎没有什么感觉……

To get the count of pairs if the data is not in order, first one needs to get the pairs by X. Create a unique list of X and use this formula: 要获得数据不正确时的对数,第一个需要通过X获得对。创建X的唯一列表并使用以下公式:

=CHAR(AGGREGATE(15,6,CODE($B$2:$B$9)/($A$2:$A$9=D2),1))&","&CHAR(AGGREGATE(15,6,CODE($B$2:$B$9)/($A$2:$A$9=D2),2))

Note this only works if the Y is one character like your data. 请注意,这仅在Y是像您的数据这样的一个字符时才有效。

在此处输入图片说明

Then create a unique list of that output and use a standard COUNTIF: 然后创建该输出的唯一列表,并使用标准COUNTIF:

=COUNTIF(E:E,G2)

在此处输入图片说明

Or you can use a pivot table on the first helper columns to get the same without the need of getting the unique pairings. 或者,您可以在第一个帮助器列上使用数据透视表来获取相同的数据,而无需获取唯一的配对。

If you use a helper column, you can combine the data into something you can count. 如果使用帮助器列,则可以将数据合并为可以计数的内容。 Use the formula =IF(A2<>A1,B2,C1&","&B2) in column C. So your data then becomes: 在C列中使用公式=IF(A2<>A1,B2,C1&","&B2) 。因此,您的数据将变为:

X Y 
1 A A
1 D A,D
2 A A
2 C A,C
3 B B
3 C B,C
4 A A
4 D A,D

Then it's a matter of counting up the combinations. 然后就可以算出组合数了。 Here's a snapshot of my example: 这是我的示例的快照:

在此处输入图片说明

You can use the MMULT function for this purpose, like this (You can insert line breaks with Alt+Enter but you do not have to, it also works if you write it on the same line): 您可以为此使用MMULT函数,例如(您可以使用Alt + Enter插入换行符,但不必这样做,如果您在同一行上编写它也可以使用):

=SUMPRODUCT(
  --($B$2:$B$19=  LEFT($D2,FIND(",",$D2)-1)  ),
  MMULT(
    --($A$2:$A$19=TRANSPOSE($A$2:$A$19)),
    --($B$2:$B$19=  MID($D2,FIND(",",$D2)+1,LEN($D2))  )))

You have to enter this as an array formula (After typing, press Ctrl+Shift+Enter, instead of just Enter; you will see the formula inside braces, like this: {=...}). 您必须将其作为数组公式输入(键入后,按Ctrl + Shift + Enter,而不仅仅是Enter;您将在括号内看到该公式,例如:{= ...})。

Here is a screenshot to show what the ranges in the formula mean: 这是一个截图,显示了公式中的范围是什么意思: Excel屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM