mergecats - Merge categories in categorical array - MATLAB (original) (raw)

Merge categories in categorical array

Syntax

Description

B = mergecats([A](#bt00kj0-1-A),[oldcats](#bt00kj0-1-oldcats)) merges two or more categories of a categorical array into one category. By default,mergecats merges all the categories listed inoldcats into a new category with the same name asoldcats(1). If an element of A belongs to a category listed in oldcats, then the corresponding element ofB belongs to oldcats(1).

example

B = mergecats([A](#bt00kj0-1-A),[oldcats](#bt00kj0-1-oldcats),[newcat](#bt00kj0-1-newcat)) merges all the categories listed in oldcats into a single new category that has the name specified by newcat.

example

Examples

collapse all

Create a categorical array that has categories for various colors.

A = categorical(["pink" "blue" "pink" "red" "blue" "red"])

A = 1×6 categorical pink blue pink red blue red

Display the categories of A. The three categories are in alphabetical order.

ans = 3×1 cell {'blue'} {'pink'} {'red' }

Merge the categories red and pink into the category red. Specify red first in oldcats to use it as the merged category. mergecats replaces the value pink with red.

oldcats = ["red" "pink"]; B = mergecats(A,oldcats)

B = 1×6 categorical red blue red red blue red

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

ans = 2×1 cell {'blue'} {'red' }

Create a categorical array that has categories for various items of clothing.

A = categorical(["shirt" "pants" "shoes" "shirt" "dress" "belt"])

A = 1×6 categorical shirt pants shoes shirt dress belt

Display the categories of A. If you do not specify an order, then the categories are in alphabetical order.

ans = 5×1 cell {'belt' } {'dress'} {'pants'} {'shirt'} {'shoes'}

Merge the categories belt and shoes into a new category called other. The value other replaces all instances of belt and shoes.

B = mergecats(A,["belt" "shoes"],"other")

B = 1×6 categorical shirt pants other shirt dress other

Display the categories of B. The new array has four categories and the order is no longer alphabetical. other appears in place of belt because it is the first category that it replaces.

ans = 4×1 cell {'other'} {'dress'} {'pants'} {'shirt'}

Create an ordinal categorical array.

A = categorical([1 2 3 2 1],1:3,["poor" "fair" "good"],Ordinal=true)

A = 1×5 categorical poor fair good fair poor

Display the categories of A. The categories have the mathematical ordering poor < fair < good.

ans = 3×1 cell {'poor'} {'fair'} {'good'}

Merge all fair or poor values into a new category called bad. Because A is ordinal, the categories to be merged must be consecutive. If they are not consecutive, then mergecats cannot create a new set of categories that have a mathematical ordering.

B = mergecats(A,["fair" "poor"],"bad")

B = 1×5 categorical bad bad good bad bad

Display the categories of B. The new array has two categories with the mathematical ordering: bad < good.

ans = 2×1 cell {'bad' } {'good'}

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

C = categorical(["Y" "Yes" "Yeah" "N" "No" "Nope"])

C = 1×6 categorical Y Yes Yeah N No Nope

ans = 6×1 cell {'N' } {'No' } {'Nope'} {'Y' } {'Yeah'} {'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.

Merge all categories whose names start with Y into one category named yes. Then merge all the categories whose names start with N into one category named no. As a result, values that have the same meaning are all in the same category. Now, C has only two categories.

C = mergecats(C,"Y" + wildcardPattern,"yes"); C = mergecats(C,"N" + wildcardPattern,"no")

C = 1×6 categorical yes yes yes no no no

ans = 2×1 cell {'no' } {'yes'}

Input Arguments

collapse all

Input array, specified as a categorical array.

Categories to merge, specified as a string array, cell array of character vectors, or pattern scalar.

New category, specified as a string scalar or a character vector.

Extended Capabilities

expand all

Themergecats function fully supports tall arrays. For more information, see Tall Arrays.

Version History

Introduced in R2013b