recodeCatLabels

Purpose

Change categorical variable labels.

Format

X_new = recodeCatLabels(X, old_labels, new_labels[, column])
Parameters:
  • X (NxK dataframe) – data with metadata.

  • old_labels (Mx1 string array) – Categorical labels to be changed.

  • new_labels (Mx1 string array) – New labels use to replace the existing labels in old_labels.

  • column (scalar or string) – Optional argument, the name or index of the variable be recoded.

Returns:

X_new (NxK dataframe) – Data in X with categorical labels in old_labels replaced by those specified in new_labels for the variable specified by column.

Remarks

  • To change the order of category labels, use reordercatlabels().

  • To set the base case, use setbasecat().

  • The key -2147483648 is reserved, and usage could result in undefined behavior.

Examples

// Load data
fname = getGAUSSHome("examples/yarn.xlsx");
yarn = loadd(fname, "cat(yarn_length) + cycles");

// Get column labels for yarn_length
{ labels, keys } = getColLabels(yarn, "yarn_length");

// Print results
print "Original yarn_length labels:";
print ntos(keys)$~labels;

// Recode yarn_length variable from
// 'low', 'medium', and 'high'
//  to 'sm', 'md', 'lg'
yarn_recoded = recodecatlabels(yarn, "low"$|"med"$|"high", "sm"$|"md"$|"lg", "yarn_length");

// Get column labels for yarn_length
{ labels, keys } = getColLabels(yarn_recoded, "yarn_length");

// Print results
print "";
print "Recoded yarn labels";
print ntos(keys)$~labels

the above code will print out the following:

Original yarn_length labels:

          0             high
          1              low
          2              med

Recoded yarn labels:

          0               lg
          1               sm
          2               md