【问题标题】:Counting the pairs that come together with a high value in a dataset计算数据集中具有高值的对
【发布时间】:2018-02-11 18:08:20
【问题描述】:

我有一组数据,列标题为 A、B、C、D、E ... K。在单元格中,值介于 0-6 之间。我正在寻找一种方法来计算和列出具有高值 (4,5,6) 的对或三元组。

例如,如果 A 列和 B 列在同一行中分别有 5 和 6,则应将其计入出现次数的计算中。如果是 1 和 6、1 和 5 等,则应跳过。仅当两者(可以超过 2 列)在同一行上具有高值时才应计入。

基本上,如果它们在同一行中具有高值,我想计算并列出列。我对所有类型的解决方案持开放态度。如果有人指导我如何做到这一点,我将不胜感激。谢谢。

示例输出:

Pairs   Number of Occurrences (can be (5,6), (4,6),(5,5), (4,5), (6,6))         
AB          10          
BC          20          
CE          30      

这是我的数据的图片。 这只是我实际数据的一部分。不是完整的列表。对不起,我说的是0到6之间的值。我删除了0,现在它们都是空白的。

A   B   C   D   E   F   G   H   I   J   K   L   M
3   3   2   4   2   4   5   4   2   2   4   3   3
2   4   3   3   3   3   6   4   2   3   3   2   4
3   3   2   4   2   4   3   3   3   3   3   3   3
3   3   4   2   4   2   4   3   3   5   1   3   3
2   4   4   2   4   2   3   6       4   2   2   4
2   4   2   4   2   4   3   3   3   3   3   2   4
3   3   2   4   2   4   3   3   3   3   3   3   3
5   1   2   4   2   4   3   3   3   3   3   5   1
2   4   1   5   1   5   3   4   2   3   3   2   4
3   3   2   4   2   4   3   3   3   3   3   3   3
5   1   2   4   2   4   2   3   3   3   3   5   1
3   3   2   4   2   4   3   4   2   4   2   3   3
4   2   3   3   3   3   3   3   3   4   2   4   2
3   3   3   3   3   3   3   3   3   6   0   3   3
2   4   3   3   3   3   3   4   2   5   1   2   4
4   2   2   4   2   4   3   1   5   3   3   4   2
2   4   4   2   4   2   4   3   3   3   3   2   4
3   3   2   4   2   4   3   2   4   4   2   3   3
3   3   4   2   4   2   4   3   3   3   3   3   3
4   2   2   4   2   4   3   3   3   3   3   4   2
2   4   3   3   3   3   3   3   3   4   2   2   4
2   4   2   4   2   4   2   2   4   4   2   2   4
4   2   3   3   3   3   5   4   2   1   5   4   2
3   3   3   3   3   3   3   4   2   3   3   3   3
1   5   2   4   2   4   3   4   2   2   4   1   5
5   1   4   2   4   2   6   1   5   3   3   5   1
4   2   1   5   1   5   3   3   3   2   4   4   2
1   5   2   4   2   4   1   3   3   3   3   1   5
2   4   4   2   4   2   1   2   4   2   4   2   4
4   2   5   1   5   1   2   4   2   3   3   4   2
4   2   1   5   1   5   4   1   5   4   2   4   2
2   4   3   3   3   3   3   3   3   6   0   2   4
4   2   2   4   2   4   3   3   3   3   3   4   2

【问题讨论】:

  • 例如,如果A和columns在同一行中分别有5和6,那么它应该计入出现次数的计算中。什么?不知道你在问什么。你能给出示例输出吗?
  • 我很抱歉解释。我更新了我的帖子,并添加了一个示例输出。这只是一个示例表。只要它给出结果,它就可以是你想要的任何东西。
  • 那么,对于每一对列,您想知道有多少对(4,5)、(4,6)或(5,6)?
  • 是的。这就是我需要的。
  • 是的。但我不知道哪一对价值高.. ab、cd 等。

标签: excel vba formula


【解决方案1】:

我制作了两个列出列对的辅助列,然后使用此公式计算 (4,5)(4,6)(5,6) 的对。

= SUMPRODUCT(COUNTIFS(INDEX($A:$M,0,MATCH(O2,$A$1:$M$1,0)),{4,4,5,5,6,6},
                      INDEX($A:$M,0,MATCH(P2,$A$1:$M$1,0)),{5,6,6,4,4,5}))

编辑 根据您最近的评论,公式更新为:

= COUNTIFS(INDEX($A:$M,0,MATCH(O2,$A$1:$M$1,0)),">3",
           INDEX($A:$M,0,MATCH(P2,$A$1:$M$1,0)),">3"))

请看下面的例子,我没有对每一列都这样做,但给了它一个好的开始:

请注意,您的原始数据在我的电子表格的左侧,我没有在这里显示它只是为了节省空间。

【讨论】:

  • 非常感谢,@ImaginaryHuman072889。它工作得很好。你拯救了我的一天:)
【解决方案2】:

这里有一个利用 Dictionary 的 VBA 解决方案(需要添加对 Microsoft Scripting Runtime 库的引用):

Option Explicit

Sub main()
    Dim col As Range
    Dim cell As Range

    Dim pairDict As Scripting.Dictionary

    Set pairDict = New Scripting.Dictionary

    With Worksheets("rates")
        With .Range("a1").CurrentRegion
            For Each col In .Columns.Resize(, .Columns.Count - 1) 'loop through referenced range columns except the last one
                .AutoFilter Field:=col.Column, Criteria1:=">4" 'filter reference range on current column with values > 4
                If Application.WorksheetFunction.Subtotal(103, col) > 1 Then ' if any filtered cells except header
                    For Each cell In Intersect(.Offset(, col.Column).Resize(, .Columns.Count - col.Column), .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow) 'loop through each row of filtered cells from one column right of current one to the last one
                        If cell.Value > 4 Then pairDict(.Cells(1, col.Column).Value & .Cells(1, cell.Column).Value) = pairDict(.Cells(1, col.Column).Value & .Cells(1, cell.Column).Value) + 1 ' if current cell value is >4 then update dictionary with key=combination of columns pair first row content and value=value+1
                    Next
                End If
                .AutoFilter 'remove current filter
            Next
        End With
        .AutoFilterMode = False 'remove filters headers
    End With

    If pairDict.Count > 0 Then ' if any pair found
        Dim key As Variant
        For Each key In pairDict.Keys 'loop through each dictionary key
            Debug.Print key, pairDict(key) 'print the key (i.e. the pair of matching columns first row content) and the value ( i.e. the number of occurrences found)
        Next
    End If
End Sub

【讨论】:

  • 谢谢,我按照你说的做了,但没有任何结果。我添加了对 MS 脚本字典的引用,将工作表的名称更改为“rates”。但它没有给出任何结果。我有 excel 2007。正在进行一些处理,但最后我看到了表格。
  • 结果正在通过语句Debug.Print 打印在即时窗口中。所以保持 VBA IDE 打开以查看它们
  • 非常感谢,我现在看到了。我不知道。 :)
  • 不客气。如果您希望将它们打印在工作表中,则可以使用以下语句:Range(...).Resize(pairDict.count,1).Value = Application.Transpose(pairDict.Keys) nad Range(...).Resize(pairDict.count+1,1).Value = Application.Transpose(pairDict.Items)
  • 不客气。你打算使用这个解决方案吗?
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多