add a row of summary statistics in a table

13 Ansichten (letzte 30 Tage)
alpedhuez
alpedhuez am 18 Jun. 2022
Kommentiert: the cyclist am 18 Jun. 2022
Suppose I have a table in Matlab:
Then I want to add a summary statistics (sum for the first col and average for the second col)
Then how shall I proceed?

Akzeptierte Antwort

Voss
Voss am 18 Jun. 2022
t = table({'new york';'chicago'},[10;5],[17;18],'VariableNames',{' ','visitor','temperature'})
t = 2×3 table
visitor temperature ____________ _______ ___________ {'new york'} 10 17 {'chicago' } 5 18
t(end+1,:) = {'' sum(t{:,'visitor'}) mean(t{:,'temperature'})}
t = 3×3 table
visitor temperature ____________ _______ ___________ {'new york'} 10 17 {'chicago' } 5 18 {0×0 char } 15 17.5

Weitere Antworten (1)

the cyclist
the cyclist am 18 Jun. 2022
Here is one way:
% The table
T = table([10;5],[17;18],'VariableNames',{'visitor','temperature'},'RowNames',{'New York','Chicago'});
% Numeric variables for the mean
vars = {'visitor','temperature'};
% Calculate the mean
varsMean = mean(T{:,vars});
% Append mean to table, and change the RowNames property of that row
T{end+1,vars} = varsMean;
T.Properties.RowNames{end} = 'mean';
% Display
T
T = 3×2 table
visitor temperature _______ ___________ New York 10 17 Chicago 5 18 mean 7.5 17.5
  2 Kommentare
alpedhuez
alpedhuez am 18 Jun. 2022
Thank you. But the first column is meant to be sum of elements.
the cyclist
the cyclist am 18 Jun. 2022
Oh, sorry. I misread. Looks like you got an effectively equivalent answer.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by