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
330 categorical
Height
100x1
800 double
LastName
100x1
11616 cell
Location
100x1
14208 cell
SelfAssessedHealthStatus
100x1
560 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
The findgroups function returns G, a vector of group numbers created from Smoker. The
splitapply function uses G to split Weight into two groups. splitapply applies the mean function
to each group and concatenates the mean weights into a vector.
findgroups returns a vector of group identifiers as the second output argument. The group
identifiers are logical values because Smoker contains logical values. The patients in the first group
are nonsmokers, and the patients in the second group are smokers.
smoker
smoker = 2x1 logical array
0
Split Data into Groups and Calculate Statistics
9-49
Précédent

- 393/1450

Suivant