在Excel中计算不同值 - 频率函数

发布于 2024-08-05 14:16:37 字数 568 浏览 4 评论 0原文

我的任务是计算 Excel 中某一列中不同字符串的数量。后来通过 Google 快速搜索,发现以下公式 此处

=SUM(IF(FREQUENCY(MATCH(B2:B10,B2:B10,0),MATCH(B2:B10,B2:B10,0) )>0,1))

考虑数据:

A 乙 C D 一个 乙 乙 C

现在,match 函数将返回一个数组(因为第一个参数是一个数组):

1 2 3 4 1 2 7 3

到目前为止一切顺利。我不明白的是 FREQUENCY 函数在这里如何工作,特别是它如何处理复制的 bin(例如 bin 1 在上述数据中被复制)。频率函数的结果是:

2 2 2 1 0 0 1 0 0

谢谢塔拉斯

I was tasked with counting the number of distinct strings in a column in excel. A quick Google search later yielded the following formula found here:

=SUM(IF(FREQUENCY(MATCH(B2:B10,B2:B10,0),MATCH(B2:B10,B2:B10,0))>0,1))

Consider the data:

A
B
C
D
A
B
E
C

Now, the match function would return an array (as the first argument is an array):

1
2
3
4
1
2
7
3

So far so good. What I don't understand is how the FREQUENCY function works here, in particular how it treats bins that are replicated (for example the bin 1 is replicated in the above data). The result of the frequency function is:

2
2
2
1
0
0
1
0
0

Thanks

Taras

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2024-08-12 14:16:37

编辑:我意识到您的解决方案是如何工作的 - 进行了修改以反映这一点。

FREQUENCY 正在从搜索数组中的 bin 中搜索条目。它的工作原理如下:

搜索数组:1 2 3 4 1 2 7 3

Bins:1 2 3 4 1 2 7 3

Bin 1 =>有两个1 => 2

仓 2 =>有两个2 => 2

仓 3 =>有两个3 => 2

仓 4 =>有一个 4 => 1

Bin 1 重复 =>已计 1 => 0

Bin 2 重复 =>已数 2 => 0

仓 7 =>有一个 7 => 1

Bin 3 重复 =>已数 3 => 0

似乎该解决方案正在利用频率怪癖,即它不会对同一个 bin 计数两次,因为您可能期望值为 1 的第二个 bin 也非零。但这就是它的工作原理 - 因为它只会计算第一个 bin 的出现次数,而不是重复的 bin,因此值大于零的行数将为您提供不同条目的数量。

这是您可能会发现有用的替代方法。它可用于计算不同值的数量:

假设您的字符串范围是 B2:B10。在另一列中填写

=(MATCH(B2,B$2:B2,1)-(ROW(B2)-ROW(B$2)))>0

当您向下复制时,该行应该发生变化,因此第二行应该是,例如:

=(MATCH(B3,B$2:B3,1)-(ROW(B3)-ROW(B$2)))>0

如果当前行包含字符串的第一个实例,则表示为 TRUE(如果您给它几分钟,您应该能够弄清楚它在做什么)。因此,如果您使用 COUNTIF() 计算 TRUE 的数量,那么您应该获得不同字符串的数量。

EDIT: I realised how your solution was working - amended to reflect this.

FREQUENCY is searching for entries from your bins in the search array. Here's how it's working:

Search array: 1 2 3 4 1 2 7 3

Bins: 1 2 3 4 1 2 7 3

Bin 1 => there are two 1's => 2

Bin 2 => there are two 2's => 2

Bin 3 => there are two 3's => 2

Bin 4 => there is one 4 => 1

Bin 1 repeated => 1 already counted => 0

Bin 2 repeated => 2 already counted => 0

Bin 7 => there is one 7 => 1

Bin 3 repeated => 3 already counted => 0

It almost seems that the solution is exploiting a FREQUENCY quirk, that is, it won't count the same bin twice, because you might expect the second bin with value 1 to be non-zero as well. But that's how it works -- as it will only count the number of occurrences for the first bin and not a duplicate bin, the number of rows with a value greater than zero will give you the number of distinct entries.

Here's an alternative approach which you might find useful. it can be used to calculate the number of distinct values:

Suppose your string range is B2:B10. Fill down in another column

=(MATCH(B2,B$2:B2,1)-(ROW(B2)-ROW(B$2)))>0

The row should change as you copy down, so the second row should be, for example:

=(MATCH(B3,B$2:B3,1)-(ROW(B3)-ROW(B$2)))>0

This is signalling TRUE if the current row contains the first instance of a string (if you give it a couple of minutes you should be able to work out what it's doing). Therefore, if you count the number of TRUEs with COUNTIF() then you should get the number of distinct strings.

不羁少年 2024-08-12 14:16:37

您可以使用 vba 例程:

Sub Uniques()

    Dim rng As Range
    Dim c As Range
    Dim clnUnique As New Collection

    Set rng = Range("A1:A8")

    On Error Resume Next
    For Each c In rng
        clnUnique.Add c.Value, CStr(c.Value)
    Next c
    On Error GoTo 0

    MsgBox "Number of unique values = " & clnUnique.Count

End Sub

如果您需要显示唯一的结果,您可以循环遍历集合并将值写入工作表上。

You could use a vba routine:

Sub Uniques()

    Dim rng As Range
    Dim c As Range
    Dim clnUnique As New Collection

    Set rng = Range("A1:A8")

    On Error Resume Next
    For Each c In rng
        clnUnique.Add c.Value, CStr(c.Value)
    Next c
    On Error GoTo 0

    MsgBox "Number of unique values = " & clnUnique.Count

End Sub

If you need to display the unique results, you can just loop through the collection and write the values on your worksheet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文