mergecats

Merges categories of a categorical array into one.

Category: Language Fundamentals > Data Types

Syntax

B = mergecats(A, oldcats)

Merges the categories in oldcats into the first of them.

B = mergecats(A, oldcats, newcat)

Merges them into a new category called newcat.

Description

Merging is the usual way to coarsen a classification, for instance joining several rare labels into other. The data keep their order and number; only the labels change, and the merged categories disappear from categories(B). For an ordinal array the categories to merge must be adjacent in the order.

Input arguments

A
A categorical array.
oldcats
Names of the categories to merge: a string array or cell array of character vectors.
newcat
Name of the merged category. The default is the first name in oldcats.

Output arguments

B
A categorical array of the same size; elements that had any of the merged categories now carry the merged one.

Example

Joining two categories

c = categorical({'a','b','c','a'});
m = mergecats(c, {'a','b'}, 'ab')
categories(m)
Output
m = 1×4 categorical array

     ab      ab      c      ab 
ans = 2×1 cell

    {'ab'}
    {'c' }

See also

categorical categories renamecats addcats removecats