简体   繁体   English

如何计算Excel列中的不同条目

[英]How to count distinct entries in a column in Excel

I know that the formula: {=SUM(1/COUNTIF(A1:A8,A1:A8))} will work for counting the distinct entries in the column A1 to A8. 我知道公式: {= SUM(1 / COUNTIF(A1:A8,A1:A8))}将用于计算A1至A8列中的不同条目。

But my question is, what exactly is this formula doing? 但是我的问题是,这个公式到底在做什么? I can't seem to follow the logic of this array formula. 我似乎无法遵循此数组公式的逻辑。

Assume a value x in A1 , blanks in A2:A8. 假设A1的值为x ,A2:A8中为空白。 If you use the Evaluate Formula tool, you'll see that the first step of the array formula provides an array of COUNTIF s: 如果使用“评估公式”工具,则会看到数组公式的第一步提供了一个COUNTIF的数组:

=SUM(1/{1,7,7,7,7,7,7,7})

Note that there is 1 1 , and 7 7 s, because there is one value x , and seven blank values. 请注意,存在1 1和7 7 s,因为存在一个值x和七个空白值。

Remember that (1/n)*n = 1 . 记住(1/n)*n = 1 So in this example, 所以在这个例子中

(1/7) * 7 = 1 (1/7)* 7 = 1

(1/1) * 1 = 1 (1/1)* 1 = 1

Sum these results, and it's as easy as 1 + 1 = 2 . 对这些结果求和,就像1 + 1 = 2一样简单。 :-) :-)

Using F9 really comes in handy here. 使用F9确实很方便。 Suppose the below values are in A1:A8: 假设以下值在A1:A8中:

dog
cat
dog
cat
cat
dog
cat
cat

In the formula bar highlight COUNTIF(A1:A8,A1:A8) and press F9 and you will see: 在编辑栏中,突出显示COUNTIF(A1:A8,A1:A8)并按F9,您将看到:

{3;5;3;5;5;3;5;5}

Because there are 3 dogs and 5 cats in the list, these are the numbers that are returned by the countif formula for each appropriate type. 因为列表中有3只狗和5只猫,所以这些是countif公式为每种适当类型返回的数字。

Now undo with CTRL+ Z or press ESC to start over. 现在,使用CTRL + Z撤消或按ESC重新开始。 This time highlight 1/COUNTIF(A1:A8,A1:A8) and you will see: 这次突出显示1 / COUNTIF(A1:A8,A1:A8) ,您将看到:

{0.333333333333333;0.2;0.333333333333333;0.2;0.2;0.333333333333333;0.2;0.2}

Because there are 3 dogs in the list, 1/3 produces .3333333. 因为列表中有3条狗,所以1/3产生.3333333。 Now .3333333 appears in the list in the same position that dog appears in the list. 现在,.3333333出现在列表中的位置与狗出现在列表中的位置相同。 Add up all the .3333333 and you get 1. 将所有.3333333加起来,得到1。

Do the same for cats. 对猫也一样。 5 Cats. 5只猫。 1/5 produces .2, and so on. 1/5产生0.2,依此类推。

When in doubt how a formula works, highlight portions of the formula and press F9 and you can see what it's calculating. 如果不确定公式的工作原理,请突出显示该公式的各个部分,然后按F9键,您可以看到它在计算什么。

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

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