英文:
Is there a way in Excel to read through 3 columns simultaneously and add the count of specific occurrences in a new column? (Complex, see description)
问题
除了我之前提出的问题:https://stackoverflow.com/questions/76671196/is-there-a-process-in-excel-that-can-automate-reading-through-a-column-containin,我现在在这个过程中迈出了下一个更大的步骤。
我有一个非常庞大的电子表格,包含超过 50,000 行。它们是来自相机陷阱的事件记录。多亏了我上次在这里提出的问题的解决方案,我现在有了一个新的列名叫 "事件编号",根据相机陷阱上是否触发了新事件而包含递增的数字(这些数字不是唯一的,因为每个事件有多行/相机图像,因此每个数字都有多个出现)。但是现在我必须找出这些事件中特定事件的发生次数。这些事件是相机图像上雄性和雌性动物的记录行为。有一组包含 11 个行为的行为,都以单词文本值的形式记录。
我开始在两张表格中手动完成这项工作,但意识到这将花费相当多的时间(有超过 2000 个事件),因为我实际上只是手动计数,然后将其写入第二张表格。是否可以使用Excel中的VBA、宏或公式等方式自动化这个过程?请参阅下面的示例:
表1:
NewEvent | EventNum | Male Behaviour | Female Behaviour |
---|---|---|---|
TRUE | 1 | PASS BY | |
FALSE | 1 | SNIFF | |
FALSE | 1 | URINATE | |
FALSE | 1 | SNIFF | |
TRUE | 2 | EAT | |
FALSE | 2 | COURT | |
FALSE | 2 | EAT | |
TRUE | 3 | PASS BY | |
FALSE | 3 | SCRATCH | |
FALSE | 3 | SNIFF | |
FALSE | 3 | SCRATCH | |
FALSE | 3 | PASS BY |
表2:
EventNum | Male Sniff | Male Scratch | Male Urinate | Male Court | Male Pass By | Male Eat | Female Sniff | Female Scratch | Female Urinate | Female Court | Female Pass By | Female Eat |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | |
3 | 1 | 2 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
我已经尝试手动完成这项工作,但表中有超过 50,000 行,包含超过 2000 个不同的事件,因此手动计数每一个事件的发生次数需要大量时间。如果能够更快地使用Excel中的过程来完成这项工作,那将是极好的。这比仅仅计算特定文本出现的次数要复杂一些,因为它还必须考虑这些行的相应事件编号。感谢您的帮助!
英文:
In addition to this previous question I asked: https://stackoverflow.com/questions/76671196/is-there-a-process-in-excel-that-can-automate-reading-through-a-column-containin, I now have the next larger step in the process.
I have a very large spreadsheet containing over 50,000 rows. They are records of events from a camera trap. Thanks to the solution from the last question I asked here, I now have a new column called "Event Number" which contains incremental numbers based on whether or not a new event was triggered on the camera trap (these are not unique numbers and there are multiple occurrences of each number as there are multiple rows/camera images for each event). But I now have to find the count of certain occurrences within those events. These occurrences are the recorded behaviours of both male and female animals on the camera images. There are a set of 11 behaviours that are all recorded as one word text values.
I started doing this manually in two sheets but realized it will take me a considerable amount of time (there are over 2000 events) as I was really just counting manually and then writing this in the second sheet. Is there any way to automate this using VBA or macro or formulas etc in Excel? Please see the example below:
SHEET 1:
NewEvent | EventNum | Male Behaviour | Female Behaviour |
---|---|---|---|
TRUE | 1 | PASS BY | |
FALSE | 1 | SNIFF | |
FALSE | 1 | URINATE | |
FALSE | 1 | SNIFF | |
TRUE | 2 | EAT | |
FALSE | 2 | COURT | |
FALSE | 2 | EAT | |
TRUE | 3 | PASS BY | |
FALSE | 3 | SCRATCH | |
FALSE | 3 | SNIFF | |
FALSE | 3 | SCRATCH | |
FALSE | 3 | PASS BY |
SHEET 2:
EventNum | Male Sniff | Male Scratch | Male Urinate | Male Court | Male Pass By | Male Eat | Female Sniff | Female Scratch | Female Urinate | Female Court | Female Pass By | Female Eat |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | |
3 | 1 | 2 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
I have tried doing this manually as mentioned, but there are over 50,000 rows which contain over 2000 different events, so counting the occurrence of each of these manually is taking a great deal of time. It would be great to understand if there is a way to navigate this faster using the processes in Excel. It is a little bit more complex than just counting for certain text occurrences, as it also must take into consideration the corresponding event number of those rows. Thank you for your help!
答案1
得分: 2
Ok,COUNTIFS()
的基本原则可以应用在这里。我确信可以将它简化,但为了清晰起见:
公式在H2
中:
=COUNTIFS(B2:B13,G2:G4,C2:C13,TEXTAFTER(H1:S1," "))*(TEXTBEFORE(C1," ")=TEXTBEFORE(H1:S1," "))+COUNTIFS(B2:B13,G2:G4,D2:D13,TEXTAFTER(H1:S1," "))*(TEXTBEFORE(D1," ")=TEXTBEFORE(H1:S1," "))
英文:
Ok, so the very basic principle of COUNTIFS()
can be applied here. I'm sure it can be golfed down, but to make it clear:
Formula in H2
:
=COUNTIFS(B2:B13,G2:G4,C2:C13,TEXTAFTER(H1:S1," "))*(TEXTBEFORE(C1," ")=TEXTBEFORE(H1:S1," "))+COUNTIFS(B2:B13,G2:G4,D2:D13,TEXTAFTER(H1:S1," "))*(TEXTBEFORE(D1," ")=TEXTBEFORE(H1:S1," "))
答案2
得分: 2
这个任务也可以使用 POWER QUERY 来执行。以下是详细步骤:
- 首先将源范围转换为一个表格,并相应地命名,例如,我已将其命名为
AnimalBehavetbl
- 接下来,从 Data 选项卡打开一个空白查询 --> 获取和转换数据 --> 获取数据 --> 从其他来源 --> 空白查询
- 以上步骤会打开 Power Query 窗口,然后从 主页 选项卡 --> 高级编辑器 --> 粘贴以下 M-Code,并删除您看到的任何内容,然后点击 完成
let
Source = Excel.CurrentWorkbook(){[Name="AnimalBehavetbl"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"NewEvent", "EventNum"}, "Attribute", "Value"),
#"Replaced Value" = Table.ReplaceValue(#"Unpivoted Other Columns","Behaviour","",Replacer.ReplaceText,{"Attribute"}),
#"Merged Columns" = Table.CombineColumns(#"Replaced Value",{"Attribute", "Value"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "NewEvent", List.Count)
in
#"Pivoted Column"
- 最后,将其导入回 Excel --> 单击 关闭并加载 或 关闭并加载到 --> 单击第一个选项将创建一个新的工作表格,其中包含所需的输出,而选择第二个选项将提示您选择结果放在哪里。
英文:
This task can be performed using <kbd>POWER QUERY</kbd> as well. The steps are outlined below:
- First convert the source range into a table and name it accordingly, for this example I have named it as
AnimalBehavetbl
- Next, open a blank query from <kbd>Data</kbd> Tab --> <kbd>Get & Transform Data</kbd> --> <kbd>Get Data</kbd> --> <kbd>From Other Sources</kbd> --> <kbd>Blank Query</kbd>
- The above lets the Power Query window opens, now from <kbd>Home</kbd> Tab --> <kbd>Advanced Editor</kbd> --> And paste the following <kbd>M-Code</kbd> by removing whatever you see, and press <kbd>Done</kbd>
let
Source = Excel.CurrentWorkbook(){[Name="AnimalBehavetbl"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"NewEvent", "EventNum"}, "Attribute", "Value"),
#"Replaced Value" = Table.ReplaceValue(#"Unpivoted Other Columns","Behaviour","",Replacer.ReplaceText,{"Attribute"}),
#"Merged Columns" = Table.CombineColumns(#"Replaced Value",{"Attribute", "Value"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "NewEvent", List.Count)
in
#"Pivoted Column"
- Lastly, to import it back to Excel --> Click on <kbd>Close & Load</kbd> or <kbd>Close & Load To</kbd> --> The first one which clicked shall create a new sheet with the required output while the latter will prompt a window asking you where to place the result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论