setcolnames

Purpose

Set column variable names.

Format

x_meta = setColNames(X, varnames[, columns])
Parameters:
  • X (NxK matrix or dataframe) – data.
  • varnames (Mx1 vector) – Names to apply to columns specified in index.
  • columns (Mx1 vector) – Optional argument, indices of columns in X to be assigned names. Default = all columns.
Returns:

x_meta (NxK dataframe) – Data with column names in varnames assigned to the columns in columns.

Examples

x = { 1  15  15,
      5  19   8,
     16  14   4,
      7  19   6 };

// Create new dataframe x_meta with the specified variable names
x_meta = setColNames(x, "Planes"$|"Trains"$|"Automobiles");

print x_meta;

The above code will print out:

Planes  Trains  Automobiles
     1      15           15
     5      19            8
    16      14            4
     7      19            6

Below we change the name of an existing variable.

// Change variable name of first column of x_meta
x_meta = setColNames(x_meta, "Airplanes", "Planes");
Airplanes  Trains  Automobiles
        1      15           15
        5      19            8
       16      14            4
        7      19            6

See also

Functions getColNames(), setColMetadata()