How to | Perform Calculations on Columns of Data

You will often need to perform calculations on the columns in a dataset, particularly when the columns represent variables. While some functions automatically operate on columns of data when a rectangular array is given, others may require some manipulation of the data in order to operate on the columns.

Create some data to work with (SeedRandom ensures a predictable result):

In[1]:=
Click for copyable input
Out[1]=

The Wolfram Language characterizes data by grouping lists within other lists. Every list is interpreted as a row within the matrix of data:

In[2]:=
Click for copyable input
Out[2]//MatrixForm=

The Grid function displays data in the same fashion, only without the braces:

In[3]:=
Click for copyable input
Out[3]=

By default many functions operate on each column when a rectangular list of lists is given as the argument.

Find the mean of each column:

In[4]:=
Click for copyable input
Out[4]=

Find the standard deviation of the columns:

In[5]:=
Click for copyable input
Out[5]=

Find the median of each column:

In[6]:=
Click for copyable input
Out[6]=

You can also select individual columns for calculations. Here, the first column from is selected:

In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=
In[10]:=
Click for copyable input
Out[10]=

For matrices with more than two columns, plot the rows as separate datasets:

In[11]:=
Click for copyable input
Out[11]=

Plot the columns by transposing the data:

In[12]:=
Click for copyable input
Out[12]=

For functions that operate on vectors, map the function onto the transposed data to operate on columns:

In[13]:=
Click for copyable input
Out[13]=

Transpose the result to get a matrix with normalized columns:

In[14]:=
Click for copyable input
Out[14]=

Transposing and mapping can also be done for functions that flatten their argument:

In[15]:=
Click for copyable input
Out[15]=