reorderCatLabels#

Purpose#

Change the order of categorical variable labels.

Format#

x_new = reorderCatLabels(x, labels[, column])#
Parameters:
  • x (NxK dataframe) – data with metadata.

  • labels (Mx1 string array) – Category labels list in their desired order.

  • column (scalar or string) – Optional argument, variables be recoded.

Returns:

x_new (NxK dataframe) – Data in x with categorical labels for the variable specified by column in the order specified by labels.

Remarks#

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;

// Reorder yarn_length variable from
// 'low', 'med', and 'high'
//  to 'high', 'med', 'low'
yarn_recoded = reorderCatLabels(yarn, "high"$|"med"$|"low", "yarn_length");

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

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

The above code will print out:

Original yarn_length labels

         0             high
         1              low
         2              med

Reordered yarn_length labels

         0             high
         1              med
         2              low