Count frequencies of occurence using both text and number

1 回表示 (過去 30 日間)
Beth Lewis
Beth Lewis 2021 年 8 月 17 日
編集済み: Beth Lewis 2021 年 8 月 17 日
Hi there,
I'll give a brief example of my table:
Fish species: Count:
red fish 2
red fish 4
red fish 9
blue fish 11
blue fish 2
blue fish 2
blue fish 1
yellow fish 2
yellow fish 10
etc etc...
What I am trying to do is to find the sum of all 'red fish' 'blue fish' etc with about 500 rows and do it automatically not manually.
So I want the code to finish saying this:
red fish 15
blue fish 16
yellow fish 12
for all 29 fish species...
Help?

採用された回答

Adam Danz
Adam Danz 2021 年 8 月 17 日
編集済み: Adam Danz 2021 年 8 月 17 日
Use groupsummary
% Produce table
% I use categorical variables but you can you strings or char-vectors
rng('default') % for reproducibility, do no include in your implementation
cats = categorical(["red fish","blue fish"])';
fishList = cats(randi(2,500,1));
counts = randi(10,500,1);
T = table(fishList, counts, 'VariableNames', {'Fish','Counts'});
% Show first few rows of table
head(T)
ans = 8×2 table
Fish Counts _________ ______ blue fish 6 blue fish 6 red fish 9 blue fish 3 blue fish 4 red fish 2 red fish 10 blue fish 7
% Count each fish group
groupsummary(T,'Fish')
ans = 2×2 table
Fish GroupCount _________ __________ blue fish 243 red fish 257
  1 件のコメント
Beth Lewis
Beth Lewis 2021 年 8 月 17 日
編集済み: Beth Lewis 2021 年 8 月 17 日
This is great, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by