How do I find the mean and standard deviation of each column for this data?

25 views (last 30 days)
what code would produce a table of the mean and standard deviation

Answers (2)

Ameer Hamza
Ameer Hamza on 9 Nov 2020
Try this
data = readtable('banking_data.csv');
idx = cellfun(@(x) isa(x, 'double'), table2cell(data(1, :)));
data = data{:,idx};
data_mean = mean(data);
data_std = std(data);

Steven Lord
Steven Lord on 9 Nov 2020
If you've read this data into a table array you can extract those variables in the table that contain numeric data then use varfun to perform an operation on each variable in the extracted table.
% Sample table
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
head(patients)
ans = 8x8 table
LastName Gender Age Height Weight Smoker Systolic Diastolic ____________ __________ ___ ______ ______ ______ ________ _________ {'Smith' } {'Male' } 38 71 176 true 124 93 {'Johnson' } {'Male' } 43 69 163 false 109 77 {'Williams'} {'Female'} 38 64 131 false 125 83 {'Jones' } {'Female'} 40 67 133 false 117 75 {'Brown' } {'Female'} 49 64 119 false 122 80 {'Davis' } {'Female'} 46 68 142 false 121 70 {'Miller' } {'Female'} 33 64 142 true 130 88 {'Wilson' } {'Male' } 40 68 180 false 115 82
% Use vartype to extract just numeric data (Age, Height, Weight, Systolic, Diastolic)
numericData = patients(:, vartype('numeric'));
head(numericData) % note no LastName, Gender, or Smoker variables
ans = 8x5 table
Age Height Weight Systolic Diastolic ___ ______ ______ ________ _________ 38 71 176 124 93 43 69 163 109 77 38 64 131 125 83 40 67 133 117 75 49 64 119 122 80 46 68 142 121 70 33 64 142 130 88 40 68 180 115 82
% Take the mean and std of each variable in the smaller table numericData
meanData = varfun(@mean, numericData)
meanData = 1x5 table
mean_Age mean_Height mean_Weight mean_Systolic mean_Diastolic ________ ___________ ___________ _____________ ______________ 38.28 67.07 154 122.78 82.96
stdData = varfun(@std, numericData)
stdData = 1x5 table
std_Age std_Height std_Weight std_Systolic std_Diastolic _______ __________ __________ ____________ _____________ 7.2154 2.8365 26.571 6.7128 6.9325

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!