%% Bin Data to Create Grouping Variable
% Group medical patients based on their age, and for each age group, create a box chart of diastolic blood pressure values.
% Load the patients data set. The Age and Diastolic variables contain the ages and diastolic blood pressure levels of 100 patients.

load patients

% Group the patients into five age bins. Find the minimum and maximum ages, and then divide the range between them into five-year bins. Bin the values in the Age variable by using the discretize function. Use the bin names in bins. The resulting groupAge variable is a categorical variable.

min(Age)
max(Age)
binEdges = 25:5:50;
bins = {'late 20s','early 30s','late 30s','early 40s','late 40s+'};
groupAge = discretize(Age,binEdges,'categorical',bins);

% Create a box chart for each age group. Each box chart shows the diastolic blood pressure values of the patients in that group.

boxchart(groupAge,Diastolic)
xlabel('Age Group')
ylabel('Diastolic Blood Pressure')

% Copyright 2019 The MathWorks, Inc.