Tips for data manipulation in Mathematica

G562 Geometric Morphometrics
Tips for data manipulation in Mathematica
Flatten[ ] - Takes a nested list and flattens it into a single list.
Partition[ ] - Opposite of flatten: groups items into a nested list.
Transpose[ ] - Switches columns to rows and rows to columns.
Department of Geological Sciences | Indiana University
(c) 2012, P. David Polly
G562 Geometric Morphometrics
Taking rows and columns of data in Mathematica
Example 1: take column 2 of row 5
Double square brackets after a variable
allow you to select parts of a list or
matrix. For these examples, data is a
matrix with ten rows and seven
columns.
The first number in the brackets
indicates rows, the second number
indicates columns.
Mathematica code: data[[5, 2]]
Example 2: take all of row 4
The code for taking the highlighted data
is shown beneath each example.
Mathematica code: data[[4]]
Department of Geological Sciences | Indiana University
(c) 2012, P. David Polly
G562 Geometric Morphometrics
Taking rows and columns (cont.)
Example 3: take rows 3 to the last row
Example 5: take rows 3 then 6
Mathematica code: data[[3;;]]
Mathematica code: data[[{3,6}]]
Example 4: take rows 3 through 6
Example 6: take column 1
Mathematica code: data[[3;;6]]
Mathematica code: data[[1;;,1]]
Department of Geological Sciences | Indiana University
(c) 2012, P. David Polly
G562 Geometric Morphometrics
Taking rows and columns (cont.)
Example 7: take columns 3 through the end
Example 9: take rows 4 through 8 in columns 3 through 5
Mathematica code: data[[1;;,3;;]]
Mathematica code: data[[1;;8,3;;5]]
Example 8: take columns 3 through 5
Mathematica code: data[[1;;,3;;5]]
Department of Geological Sciences | Indiana University
Example 10: take columns 3 then 6
Mathematica code: data[[1;;,{3,6}]]
(c) 2012, P. David Polly
G562 Geometric Morphometrics
Taking rows and columns (cont.)
Example 11: take columns 6 then 3
Mathematica code: data[[1;;,{6,3}]]
Department of Geological Sciences | Indiana University
Example 12: take rows 1 - 6 of columns 2, 4, and 6
Mathematica code: data[[1;;6,{2,4,6}]]
(c) 2012, P. David Polly
G562 Geometric Morphometrics
Lists with more than two dimensions
The examples so far assume that data are stored as a simple list or as a two-dimensional list (rows and columns).
In principle, data can be stored in lists with three, four, or any number of dimensions. Consider an Excel workbook
that has many worksheets, each with its own rows and columns - it is an example of a three dimensional array,
which is what it becomes when it is imported into Mathematica.
To retrieve the 3rd and 4th columns from all rows in the second “page” of a three dimensional array, the
Mathematica code would be:
data[[2, 1;;, {3,4}]]
“2” applies to the first dimension of the array, “1;;” to its second dimension, and “{3,4}” to its third dimension. It is
often helpful to build this up dimension by dimension:
data[[2]] will show you everything in the second page so you can confirm you have the right thing
data[[2, 1;;]] will show you all rows from one onward (which should be identical to the first step)
data[[2, 1;;,{3,4}]] will limit the output to just columns 3 and 4.
Department of Geological Sciences | Indiana University
(c) 2012, P. David Polly
G562 Geometric Morphometrics
Map[]
a function for repeating the same thing for every item in a list
H* three ways to put your coordinates into the Point@D function *L
coords
Out[12]=
8848.425, 90.45<, 834.1689, 74.3711<, 834.62, 75.3867<, 834.8333, 73.5978<, 838., 73.<, 833.223, 77.879<,
833.9342, 100.483<, 828.275, 90.3825<, 837.566, 83.592<, 846.413, 7.934<, 846.3544, 103.194<, 847.4393, 107.429<<
H* table works by counting out each item in the list and putting it into the Point@D funtion *L
Table@Point@coords@@xDDD, 8x, Length@coordsD<D
Out[15]=
[email protected], 90.45<D, [email protected], 74.3711<D, [email protected], 75.3867<D, [email protected], 73.5978<D,
Point@838., 73.<D, [email protected], 77.879<D, [email protected], 100.483<D, [email protected], 90.3825<D,
[email protected], 83.592<D, [email protected], 7.934<D, [email protected], 103.194<D, [email protected], 107.429<D<
H* Map@D function replaces the  with each element of coords. The & is placed at the end to indicate
that the replacement should continue until all elements in coords have been used. *L
Map@Point@D &, coordsD
Out[16]=
[email protected], 90.45<D, [email protected], 74.3711<D, [email protected], 75.3867<D, [email protected], 73.5978<D,
Point@838., 73.<D, [email protected], 77.879<D, [email protected], 100.483<D, [email protected], 90.3825<D,
[email protected], 83.592<D, [email protected], 7.934<D, [email protected], 103.194<D, [email protected], 107.429<D<
H* shortcut for Map@D. Takes each element on the right of the êû and replaces  on the left with it.
causes the replacement to repeat for all items in coords. This syntax requires much less typing than
the Table@D format above. *L
Point@D & êû coords
Out[17]=
&
[email protected], 90.45<D, [email protected], 74.3711<D, [email protected], 75.3867<D, [email protected], 73.5978<D,
Point@838., 73.<D, [email protected], 77.879<D, [email protected], 100.483<D, [email protected], 90.3825<D,
[email protected], 83.592<D, [email protected], 7.934<D, [email protected], 103.194<D, [email protected], 107.429<D<
Department of Geological Sciences | Indiana University
(c) 2012, P. David Polly