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

45 次查看(过去 30 天)
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!

回答(2 个)

Chunru
Chunru 2021-11-14
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 2021-11-16

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by