英文:
easiest way to aggregate values over multiple columns?
问题
我明白你的需求,以下是你提供的内容的翻译:
我有一个表格,看起来像这样:
分类 | 动物1 | 动物2 | 动物3 |
---|---|---|---|
哺乳动物 | 狗 | 猫 | |
鸟类 | 鹦鹉 | 猫头鹰 | |
哺乳动物 | 猫 | 斑马 | 狮子 |
鱼类 | 金鱼 | 鲔鱼 | |
鸟类 | 鸵鸟 | 鹦鹉 | 小鸡 |
我的目标是根据它们的分类制作每种动物的摘要,即仅仅获取每种动物及其对应分类的计数,如下所示:
分类 | 动物 | 计数 |
---|---|---|
哺乳动物 | 狗 | 1 |
哺乳动物 | 猫 | 2 |
哺乳动物 | 斑马 | 1 |
哺乳动物 | 狮子 | 1 |
鸟类 | 鹦鹉 | 2 |
鸟类 | 猫头鹰 | 1 |
... |
我是这样做的(这可能是一个非常初级的解决方案,我担心我把它弄得比应该复杂):
首先,我在第二列中工作。我使用数组公式、UNIQUE 和 FLATTEN 同时获取所有动物的唯一列表,如下所示:
=ARRAYFORMULA(UNIQUE(FLATTEN(B2:D)))
这样我就得到了从B列到D列的所有动物的列表。
然后,为了获取第一列的值,我在原始原始文件中添加了一个列,将3列中的动物连接起来:
分类 | 动物1 | 动物2 | 动物3 | 连接 |
---|---|---|---|---|
哺乳动物 | 狗 | 猫 | 狗, 猫 | |
鸟类 | 鹦鹉 | 猫头鹰 | 鹦鹉, 猫头鹰 | |
哺乳动物 | 猫 | 斑马 | 狮子 | 猫, 斑马, 狮子 |
鱼类 | 金鱼 | 鲔鱼 | 金鱼, 鲔鱼 | |
鸟类 | 鸵鸟 | 鹦鹉 | 小鸡 | 鸵鸟, 鹦鹉, 小鸡 |
然后,我使用索引匹配来获取分类。
从那里开始,我只是在第三列上使用COUNTIF来计算B列到D列中每种动物的实例数。
英文:
I have a table that looks like this:
classification | animal1 | animal2 | animal3 |
---|---|---|---|
mammal | dog | cat | |
bird | parrot | owl | |
mammal | cat | zebra | lion |
fish | goldfish | tuna | |
bird | ostrich | parrot | chicken |
My goal is to make a summary of each animal based on their classification, i.e. basically just to get a count of each animal and their corresponding classification such that:
classification | animal | count |
---|---|---|
mammal | dog | 1 |
mammal | cat | 2 |
mammal | zebra | 1 |
mammal | lion | 1 |
bird | parrot | 2 |
bird | owl | 1 |
... |
The way I did this (and this might be a very beginner solution, I'm afraid I'm making it more complicated than it should be) is by doing the following:
classification | animal | count |
---|---|---|
mammal | dog | 1 |
mammal | cat | 2 |
mammal | zebra | 1 |
mammal | lion | 1 |
bird | parrot | 2 |
bird | owl | 1 |
First, I work in the second column. I get the unique list of all animals using array formula, unique, and flatten at the same time like:
=ARRAYFORMULA(UNIQUE(FLATTEN(B2:D)))
This gives me a list of all the animals from columns B to D.
Then, to get the value in the first column, I add a column in the original raw file that concatenates the animals in the 3 columns:
classification | animal1 | animal2 | animal3 | concat |
---|---|---|---|---|
mammal | dog | cat | dog, cat | |
bird | parrot | owl | parrot, owl | |
mammal | cat | zebra | lion | cat, zebra, lion |
fish | goldfish | tuna | goldfish, tuna | |
bird | ostrich | parrot | chicken | ostrich, parrot, chicken |
Then I use index match to get the classification
From there, I just use a COUNTIF on the third column to count the instances of each animal in the columns B:D
答案1
得分: 1
<!-- language-all: js -->
*You may try:*
=index(let(Σ,tocol(if(len(B2:D),A2:A&"|"&B2:D,),1),
sort({split(unique(Σ),"|"),countif(Σ,unique(Σ))})))
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/dZpcI.png
英文:
<!-- language-all: js -->
You may try:
=index(let(Σ,tocol(if(len(B2:D),A2:A&"|"&B2:D,),1),
sort({split(unique(Σ),"|"),countif(Σ,unique(Σ))})))
答案2
得分: 1
你解决问题的方式没有问题。我个人尽量避免使用辅助列,尤其是对于简单的问题,所以下面是我会这样做的方式。
首先,我们进行表格的解除轴旋,然后运行 COUNTIF
,最后移除重复和空白行,并对结果进行排序。
另外,UNIQUE(FLATTEN(B2:D))
不需要使用数组公式,所以不需要在它周围添加 ARRAYFORMULA
包装。而且,现在有了 TOCOL
,它是 FLATTEN
的更好版本,我建议查看一下。
英文:
There's nothing wrong with how you solved the problem. I personally try to avoid helper columns, especially for simple problems so here's how I would do it.
=SORT(LET(cc,TOCOL(A2:A&"ζ"&B2:D),QUERY(UNIQUE({SPLIT(cc,"ζ"),COUNTIF(cc,cc)}),"WHERE Col2 IS NOT NULL")),1,0)
First we unpivot the table, then we run a COUNTIF
and finally we remove the duplicate and blank rows and sort the result.
Btw, UNIQUE(FLATTEN(B2:D))
is not an array formula so you don't need an ARRAYFORMULA
wrapper around it. Also, we have TOCOL
now, which is a better version of FLATTEN
, I recommend checking it out.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论