| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Statistics Toolbox |
| Contents | Index |
| Learn more about Statistics Toolbox |
Drop levels
B = droplevels(A)
B = droplevels(A,oldlevels)
B = droplevels(A) removes unused levels from the categorical array A. B is a categorical array with the same size and values as A, but with a list of potential levels that includes only those present in some element of A.
B = droplevels(A,oldlevels) removes specified levels from the categorical array A. oldlevels is a cell array of strings or a 2-D character matrix specifying the levels to be removed.
droplevels removes levels, but does not remove elements. Elements of B that correspond to elements of A having levels in oldlevels all have an undefined level.
Drop unused age levels from the data in hospital.mat:
load hospital
edges = 0:10:100;
labels = strcat(num2str((0:10:90)','%d'),{'s'});
AgeGroup = ordinal(hospital.Age,labels,[],edges);
AgeGroup = droplevels(AgeGroup);
getlabels(AgeGroup)
ans =
'20s' '30s' '40s' '50s'Load patient data from the CSV file hospital.dat and store the information in a dataset array with observation names given by the first column in the data (patient identification):
patients = dataset('file','hospital.dat',...
'delimiter',',',...
'ReadObsNames',true);
Make the {0,1}-valued variable smoke nominal, and change the labels to 'No' and 'Yes':
patients.smoke = nominal(patients.smoke,{'No','Yes'});
Add new levels to smoke as placeholders for more detailed histories of smokers:
patients.smoke = addlevels(patients.smoke,...
{'0-5 Years','5-10 Years','LongTerm'});
Assuming the nonsmokers have never smoked, relabel the 'No' level:
patients.smoke = setlabels(patients.smoke,'Never','No');
Drop the undifferentiated 'Yes' level from smoke:
patients.smoke = droplevels(patients.smoke,'Yes'); Warning: OLDLEVELS contains categorical levels that were present in A, caused some array elements to have undefined levels.
Note that smokers now have an undefined level.
Set each smoker to one of the new levels, by observation name:
patients.smoke('YPL-320') = '5-10 Years';addlevels, getlabels, islevel, mergelevels, reorderlevels
![]() | double (dataset) | dummyvar | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |