Computes the mode of every column of a matrix.


m = modec(x)
Parameters:x (NxK matrix) – data
Returns:m (Kx1 matrix) – contains the mode of every column of x.


Basic example

// Create a 5x1 column vector
x = { 3,
      3 };

// Compute the mode of this column
m = modec(x);

After the code above, m is equal to:


Multi-column mode from credit dataset

This example will load the variables, Cards, Age and Education from the dataset credit.dat and find the mode of each variable.

// Create the file name with full path
fname = getGAUSSHome() $+ "examples/credit.dat";

// Load three variables by name
X = loadd(fname, "Cards + Age + Education");

// Compute the mode of each of the
// three columns in 'X'
m = modec(X);

After the code above, m will equal:


Which tells us that for our sample, the most common:

  • number of credit cards owned is 2.
  • age is 44 years old.
  • number of years of education is 16.


  • If two more more numbers are tied for the most appearances, then modec() will select the smallest of them.

  • modec() will not count missing values. However, if a column contains only missing values, a missing value will be returned, indicating that no valid numbers were found.

  • To find the mode of the entire matrix, use the vecr() command to turn the matrix into a column vector before calling modec.

    m = modec(vecr(x));
  • To find out how many times each value is present, use counts().

  • To compute the mode for subgroups of a variable, use aggregate().

See also

Functions meanc(), stdc()