Main Content

removecats

Remove categories from categorical array

Description

B = removecats(A) removes categories from a categorical array. By default, removecats removes all unused categories. The output categorical array B has the same size and values as the input A. However, B might have fewer categories.

example

B = removecats(A,oldcats) removes the categories specified by oldcats. The function removes categories but does not remove any elements of the array. Therefore the elements of B are undefined where the corresponding elements of A belong to any category specified by oldcats.

example

Examples

collapse all

Create a categorical array that has some unused categories. When a categorical array has an unused category, it means that no element of the array belongs to that category.

A = categorical(["red" "blue" "blue" "blue" "red" "blue"], ...
                ["black" "blue" "red" "green"])
A = 1x6 categorical
     red      blue      blue      blue      red      blue 

Display the categories.

categories(A)
ans = 4x1 cell
    {'black'}
    {'blue' }
    {'red'  }
    {'green'}

Summarize the categorical array. The summary shows that there are no elements that belong to the categories black and green.

summary(A)
A: 1x6 categorical

     black      blue      red      green      <undefined> 
     0          4         2        0          0           

Remove the unused categories.

B = removecats(A)
B = 1x6 categorical
     red      blue      blue      blue      red      blue 

B has the same values as A.

Display the categories of B. The new categorical array has fewer categories.

categories(B)
ans = 2x1 cell
    {'blue'}
    {'red' }

Summarize B. The summary shows that it has no unused categories.

summary(B)
B: 1x6 categorical

     blue      red      <undefined> 
     4         2        0           

Create a categorical array.

A = categorical(["plane" "car" "train" "car" "plane" "car"])
A = 1x6 categorical
     plane      car      train      car      plane      car 

Display its categories.

categories(A)
ans = 3x1 cell
    {'car'  }
    {'plane'}
    {'train'}

Remove the category car. The elements that belong to that category are now undefined.

B = removecats(A,"car")
B = 1x6 categorical
     plane      <undefined>      train      <undefined>      plane      <undefined> 

Display the categories of B. It has one fewer category than A.

categories(B)
ans = 2x1 cell
    {'plane'}
    {'train'}

Create a categorical array. This array has many different categories that can stand for "yes" and "no".

C = categorical(["Y" "Yes" "N" "No" "Yes" "Y"])
C = 1x6 categorical
     Y      Yes      N      No      Yes      Y 

categories(C)
ans = 4x1 cell
    {'N'  }
    {'No' }
    {'Y'  }
    {'Yes'}

You can match multiple category names by using a pattern. For example, to specify category names that start with a Y, you can use a wildcard pattern. To create a wildcard pattern, use the wildcardPattern function.

Remove the categories whose names start with Y. The removecats function removes categories but does not remove any elements of the input array. Therefore elements that belonged to the categories whose names started with Y are now undefined values.

C = removecats(C,"Y"+wildcardPattern)
C = 1x6 categorical
     <undefined>      <undefined>      N      No      <undefined>      <undefined> 

categories(C)
ans = 2x1 cell
    {'N' }
    {'No'}

Input Arguments

collapse all

Input array, specified as a categorical array.

Categories to remove, specified as a string array, character vector, cell array of character vectors, or pattern scalar. The default is all the unused categories from A.

Tips

  • ~ismember(categories(A),unique(A)) returns logical 1 (true) for any unused category of A.

Extended Capabilities

Version History

Introduced in R2013b