Main Content

Summary Statistics Grouped by Category

Note

Thenominalandordinalarray data types are not recommended. To represent ordered and unordered discrete, nonnumeric data, use theCategorical Arraysdata type instead.

Summary Statistics Grouped by Category

This example shows how to compute summary statistics grouped by levels of a categorical variable. You can compute group summary statistics for a numeric array or a dataset array usinggrpstats.

Load sample data.

loadhospital

The dataset array,hospital, has 7 variables (columns) and 100 observations (rows).

Compute summary statistics by category.

The variableSexis a nominal array with two levels,男性andFemale. Compute the minimum and maximum weights for each gender.

stats = grpstats(hospital,'Sex',{'min','max'},'DataVars','Weight')
stats = Sex GroupCount min_Weight max_Weight Female Female 53 111 147 Male Male 47 158 202

The dataset array,stats, has observations corresponding to the levels of the variableSex. The variablemin_Weightcontains the minimum weight for each group, and the variablemax_Weightcontains the maximum weight for each group.

Compute summary statistics by multiple categories.

The variableSmokeris a logical array with value1for smokers and value0for nonsmokers. Compute the minimum and maximum weights for each gender and smoking combination.

stats = grpstats(hospital,{'Sex','Smoker'},{'min','max'},...'DataVars','Weight')
stats = Sex Smoker GroupCount min_Weight max_Weight Female_0 Female false 40 111 147 Female_1 Female true 13 115 146 Male_0 Male false 26 158 194 Male_1 Male true 21 164 202

The dataset array,stats, has an observation row for each combination of levels ofSexandSmokerin the original data.

See Also

||

Related Examples

More About