Phys 238 : Entering and Manipulating Data in Mathematica

Getting Started

Mathematica remembers definitions, even if you delete them from the notebook.  This can sometimes be confusing, so it’s often a good idea to simply clear everything and start anew.  This is especially true if you use the Evaluation -> Evaluate Notebook menu item.

Clear["Global`*"] (* Clear all variable and function definitions *)

Entering Data

Consider the data for static torque from the torsional oscillator experiment.  This data consists of {mass, angle} pairs.  There are multiple ways to enter the data. 

The data is a list of data points.  All lists are made of elements in curly braces { } separated by commas.  Here, each data point is itself a list, consisting of two numbers (in curly braces) separated by a comma.   To see what the data looks like, display it with TableForm[ ].

There are many ways to enter the data:

Typing the data directly:

rawdata = { 
{0, 2.98},
{50, 2.79},
{100, 2.59},
{150, 2.38},
{200, 2.17},
{250, 1.96},
{300, 1.66},
{350, 1.41},
{400, 1.21},
{0, 2.98},
{-50, 3.20},
{-100, 3.40},
{-150, 3.60},
{-200, 3.81},
{-250, 4.06},
{-300, 4.32},
{-350, 4.56},
{-400, 4.74},
{0, 3.00}
 }

TableForm[rawdata]

Typing the data using the Classroom Assistant:(({{□, □}, {□, □}}))

From the Palettes -> Classroom Assistant palette, look for the matrix item: (({{□, □}, {□, □}})).  (Depending on the version of Mathematica, it might be in the “Typesetting” menu, or in the “Advanced” menu.  (Use Ctrl-Enter to add a new row.)

rawdata = (({{0, 2.98}, {50, 2.79}, {100, 2.59}, {150, 2.38}, {200, 2.17}, {250, 1.96}, {300, 1.66}, {350, 1.41}, {400, 1.21}, {0, 2.98}, {-50, 3.20}, {-100, 3.40}, {-150, 3.60}, {-200, 3.81}, {-250, 4.06}, {-300, 4.32}, {-350, 4.56}, {-400, 4.74}, {0, 3.00}}))

Reading from a file:

To read the data from a text file in the same directory as your notebook, first tell Mathematica what directory to use.  I like to keep the data file in the same directory as the notebook.

SetDirectory[NotebookDirectory[]]

Typically, files might have header information (names of columns, dates, etc.)  There might also be other comments.  Sometimes the columns are separated by spaces, tabls, or commas.  It is usually helpful to see what the raw data file looks like.  The FilePrint[ ] command prints out the contents of the file so you can look at it and assess what to do next.

FilePrint["TO-static-20250212.csv"]

This particular file has 3 columns, separated by commas.  There is a single header line at the top giving the column titles.

Importing as a CSV (Comma Separated Values) file

If you know the import format, you can specify it explicitly:

rawdata = Import["TO-static-20250212.csv", "CSV"]

TableForm[rawdata]

Dimensions[rawdata] (* It has 20 rows and 3 columns *)

Selecting and Calculating With Data

No matter how you import data, you will likely need to make some adjustments or calculations.  Three very useful commands are Select[ ], Sort[ ], and Table[ ].  For simplicity, assume you import the data as a CSV file.

rawdata = Import["TO-static-20250212.csv", "CSV"];
TableForm[rawdata]

Looking at parts of an array:

The [[ ]] notation is used to look at an element of an array.

rawdata[[2]] (* The second row *)

To look at the 3rd item on the second row, use the [[ row, column ]] notation

rawdata[[2, 3]]

The special name All can also be used:

rawdata[[All, 3]] (* All rows, column 3 *)

rawdata[[All, {2, 3}]] (* All rows, columns 2 and 3. *)

rawdata[[2, All]] (* Row 2, all columns *)

The Select Command:

The basic syntax for the Select command is Select[data, criteria]  (see the online help for full details  The Select command loops through the data and returns all points for which the criteria are true.  Along the way, it sets the symbol ‘#’ equal to each element of the data.  Since # is itself a list (with 3 elements) you can look at the first, second, and third elements of each point with #[[1]], #[[2]], and #[[3]].    (You must include the ‘&’ at the end.)  You can combine tests with  ‘&&’ to mean logical “AND”.  You would use ‘||’ to mean logical OR.    Thus to select lines where the first, second, and third elements are all numbers, you could do

data = Select[rawdata, NumberQ[#[[1]]] && NumberQ[#[[2]]] && NumberQ[#[[3]]]&]

Equivalently, you can use the VectorQ function to apply a criterion to every element of a list:

data = Select[rawdata, VectorQ[#, NumberQ] &]

To select only points where the mass is between -200 and +200 (including the endpoints) you could do

somedata = Select[data, -200 <= #[[1]] <= 200 &]

Alternatively, if you know you simply want to drop the first row, you can use the Drop[ ] command.

Drop[rawdata, 1]

SortBy[data, #[[2]] &]

The SortBy Command:

(RefLink[SortBy, paclet:ref/SortBy])[list,f]  
The SortBy[ ] command sorts the elements of list in the order defined by applying f to each of them in term.  This is useful for sorting data arrays in a variety of ways.  For example, you might want to sort the data by mass values (the first element in the array).  (This uses the same # syntax as the Select command.)

sorted = SortBy[data, #[[1]] &]; (* The ';' suppresses printing this line so we can make a pretty table instead. *)
TableForm[sorted]

You can also sort by the first or last elements in each row with the ‘First’ or ‘Last’ operators instead of #[[1]] or #[[-1]].

sorted = SortBy[data, Last];
TableForm[sorted]

The Table Command:

Suppose you want to create a new table with {angle, mass} pairs reversed from the input, and you also want to convert the masses from gram to kilogram.  The Table[ ] function is a handy way to do that. 

First, note that the general syntax to generate a counter ‘i’ that runs from 1 to the length of the data is

Table[i, {i, 1, Length[data]}]

To create our desired array, instead of just printing out the counter, we want to create a list (in curly brackets) of {angle, mass} pairs.  Since angle is the second column, you use data[[i, 2]] to get angle and [[i, 1]] to get the mass.   The ‘/1000’ converts the masses to kilograms.

massvsAngle = Table[ {data[[i, 2]], data[[i, 1]]/1000. }, {i, 1, Length[data]}];

TableForm[massvsAngle] (* Check what it looks like *)

When you want to transform or manipulate your data in some way, the Table[ ] function is often a good approach.  There often are more succinct ways to write things in Mathematica, but the Table[ ] function is often the easiest to read and  understand.