Split Data into Groups and Calculate Statistics
This example shows how to split data from the patients.mat data file into groups. Then
it shows how to calculate mean weights and body mass indices, and variances in blood
pressure readings, for the groups of patients. It also shows how to summarize the results
in a table.
Load Patient Data
Load sample data gathered from 100 patients.
load patients
Convert Gender and SelfAssessedHealthStatus to categorical arrays.
Gender = categorical(Gender);
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus);
whos
Name Size Bytes Class Attributes
Age 100x1 800 double
Diastolic 100x1 800 double
Gender 100x1 346 categorical
Height 100x1 800 double
LastName 100x1 12416 cell
Location 100x1 15008 cell
SelfAssessedHealthStatus 100x1 592 categorical
Smoker 100x1 100 logical
Systolic 100x1 800 double
Weight 100x1 800 double
Calculate Mean Weights
Split the patients into nonsmokers and smokers using the Smoker variable. Calculate the
mean weight for each group.
[G,smoker] = findgroups(Smoker);
meanWeight = splitapply(@mean,Weight,G)
meanWeight = 2×1
149.9091
161.9412
Split Data into Groups and Calculate Statistics