Combining Rows of a Table based on Matching first column entry?

45 vues (au cours des 30 derniers jours)
MR
MR le 14 Nov 2021
I have a table with data of coaches from different schools and I want to combine rows that have matching school names. I'm not sure if there's a way to do this without looping but that would be ideal for me. Below is what a section of the Table looks like:
School Year Games Wins Losses
'Air Force' 2011 13 7 6
'Akron' 2011 12 1 11
'Alabama' 2011 13 12 1
'Arizona' 2011 6 3 3
'Arizona' 2011 6 1 5
'Arizona State' 2011 13 6 7
I would like to have the table look like this below (Combining the Row 'Arizona'):
School Year Games Wins Losses
'Air Force' 2011 13 7 6
'Akron' 2011 12 1 11
'Alabama' 2011 13 12 1
'Arizona' 2011 12 4 8
'Arizona State' 2011 13 6 7
Again, this is only 6 rows of the table, but there are ~130 total rows in the table with up to 20 rows that need to be combined into 10 rows. Any help/advice would be appreciated! Thanks in advance!

Réponses (2)

Chunru
Chunru le 14 Nov 2021
Tcell ={'Air Force' 2011 13 7 6
'Akron' 2011 12 1 11
'Alabama' 2011 13 12 1
'Arizona' 2011 6 3 3
'Arizona' 2011 6 1 5
'Arizona State' 2011 13 6 7};
T = cell2table(Tcell);
T.Properties.VariableNames = {'School' 'Year' 'Games' 'Wins' 'Losses'};
groupsummary(T, {'School', 'Year'}, 'sum')
ans = 5×6 table
School Year GroupCount sum_Games sum_Wins sum_Losses _________________ ____ __________ _________ ________ __________ {'Air Force' } 2011 1 13 7 6 {'Akron' } 2011 1 12 1 11 {'Alabama' } 2011 1 13 12 1 {'Arizona' } 2011 2 12 4 8 {'Arizona State'} 2011 1 13 6 7

Seth Furman
Seth Furman le 16 Nov 2021

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by