This page describes the functions used in class and in the textbook for working with tables in Pyret.
In the Pyret documentation, you may see other ways of manipulating tables, but we expect you to use the functions listed on this page.
Getting started
Always start by adding this include
statement at the top
of your file:
include sharedgdrive("dcic2021", "1wyQZj_L0qqV9Ekgr9au6RX2iqt2Ga8Ep")
Creating a Table from scratch
To define a Table
, we can write it using this syntax:
table: col0name, col1name, ... row: r0c0, r0c1, ... row: r1c0, r1c1, ... row: r2c0, r2c1, ... ... end
For example,
mytable = table: name, age, favoritecolor row: "Bob", 12, "blue" row: "Alice", 17, "green" row: "Eve", 13, "red" end
The Table mytable
defined here
is used for many of the examples below. Evaluating
mytable
in the interactions pane after running the
definition above will display a formatted version of the
Table
:
››› mytable
Accessing a Row or value in a Table
Getting a row
Remember to begin counting from 0 rather
than 1.
To get a specific row number from the table, use
rown
, e.g.,
››› mytable.rown(0)
Getting a value
The syntax row[colname]
accesses a Row
at a particular column, resulting in a particular value, e.g.,
››› mytable.rown(0)["age"] 12 ››› alicerow = mytable.rown(1) ››› alicerow["favoritecolor"] "green"
Creating and extracting Tables

filterwith(t :: Table, keep :: (Row > Boolean)) > Table

Given a
Table
and a predicate on rows, returns aTable
with only the rows for which the predicate returnstrue
.Example: Suppose we wanted to keep only rows of
mytable
that have an"age"
value less than 15. Here is an example “predicate” function (the input namedkeep
forfilterwith
):fun ageunderfifteen(r :: Row) > Boolean: doc: "Return true if the Number in row r, column 'age' is less than 15" r["age"] < 15 end
››› filterwith(mytable, ageunderfifteen) name age favoritecolor "Bob" 12 "blue" "Eve" 13 "red"

orderby(t :: Table, colname :: String, sortup :: Boolean) > Table

Given a
Table
and the name of a column in thatTable
, return aTable
with the same rows ordered based on the named column. Ifsortup
istrue
, theTable
will be sorted in ascending order, otherwise it will be in descending order.››› orderby(mytable, "age", true) name age favoritecolor "Bob" 12 "blue" "Eve" 13 "red" "Alice" 17 "green"
››› orderby(mytable, "age", false) name age favoritecolor "Alice" 17 "green" "Eve" 13 "red" "Bob" 12 "blue"
buildcolumn(t :: Table, colname :: String, builder :: (Row > A)) > Table

Takes an existing
Table
and produces a newTable
with an additional column with the givencolname
, usingbuilder
to produce the values for that column, once for each row.Here,
A
is the type of the new column, determined by the type of value thebuilder
function returns.Example: Suppose we wanted to create a column that tells us whether or not the value in each
Row
’s"age"
column is in the “teens” (i.e., thirteen to nineteen). Here is an examplebuilder
function:fun isteenagerbuilder(r :: Row) > Boolean: doc: "Return true if the Number in row r, column 'age' is > 12 and < 20" (r["age"] > 12) and (r["age"] < 20) end
››› buildcolumn(mytable, "isteenager", isteenagerbuilder) name age favoritecolor isteenager "Bob" 12 "blue" false "Alice" 17 "green" true "Eve" 13 "red" true
addrow(t :: Table, r :: Row) > Table

Takes a
Table
and aRow
to add and produces a newTable
with the rows from the original table followed by the givenRow
.Example: Suppose we have this other table, inspiringly named
othertable
, and we want to add its secondRow
to our original table,mytable
:othertable = table: name, age, favoritecolor row: "Julia", 21, "marigold" row: "Colton", 19, "blue" row: "Monica", 20, "purple" row: "Giselle", 21, "red" end
››› newrow = othertable.rown(1) ››› addrow(mytable, newrow) name age favoritecolor "Bob" 12 "blue" "Alice" 17 "green" "Eve" 13 "red" "Colton" 19 "blue"
addcol(t :: Table, colname :: String, cvals :: List<Any>) > Table

Takes a
String
representing a column name and aList
of values and produces a newTable
with the columns of the inputTable
followed by a column with the given name and values. Note that the length ofcvals
must equal the length of theTable
.Example: Here’s a
List<String>
representing hair colors, which we want to add tomytable
as a column:››› haircolorcvals = [list: "brown", "red", "blonde"] ››› addcol(mytable, "haircolor", haircolorcvals) name age favoritecolor haircolor "Bob" 12 "blue" "brown" "Alice" 17 "green" "red" "Eve" 13 "red" "blonde"
selectcolumns(t :: Table, colnames :: List<String>) > Table

Takes a
Table
and aList<String>
containing column names, and produces a newTable
containing only those columns. The order of the columns is as given in the inputList
.Example:
››› desiredcolnames = [list: "name", "favoritecolor"] ››› selectcolumns(mytable, desiredcolnames) name favoritecolor "Bob" "blue" "Alice" "green" "Eve" "red"
transformcolumn(t :: Table, colname :: String, f :: (A > B)) > Table

Takes a
Table
, aString
representing a column name, and a transformation function and produces a newTable
where the transformation function has been applied to all values in the named column. The values in the original column are of typeA
(the input type of the function) and values in the new column have typeB
(the output type of the function).Example: Suppose the rows of
mytable
represent a family whose last name is “Smith”. We want to change each name in the"name"
column to whateverString
is currently in the column plus theString
" Smith"
after it.Here is a transformation function for this example:
fun addlastname(name :: String) > String: doc: "Consumes a String; returns that String + ' Smith'" name + " Smith" end
››› transformcolumn(mytable, "name", addlastname) name age favoritecolor "Bob Smith" 12 "blue" "Alice Smith" 17 "green" "Eve Smith" 13 "red"
Extracting data through Table methods
Table methods are how we extract data from a table. Methods are similar in spirit to functions, but their notation – table.operation(args) – is more suggestive of going inside a table to extract data.
t.length() > Number

For the
Table
namedt
, returns aNumber
representing the number of rows in theTable
.››› mytable.length() 3
t.getcolumn(colname :: String) > List<A>

Returns a
List
of the values in the named column in theTable
namedt
.A
is the type of the data in the named column.››› mytable.getcolumn("name") [list: "Bob", "Alice", "Eve"]
t.drop(colname :: String) > Table

Returns a
Table
that is the same asTable
t
, except without the column whose name iscolname
.››› mytable.drop("age") name favoritecolor "Bob" "blue" "Alice" "green" "Eve" "red"
Summarizing columns
sum(t :: Table, colname :: String) > Number

Takes a
Table
and aString
representing the name of a column in thatTable
. Returns aNumber
representing the sum of the values in the column. Note that the given column must containNumber
values.››› sum(mytable, "age") 42
mean(t :: Table, colname :: String) > Number

Takes a
Table
and aString
representing the name of a column in thatTable
. Returns aNumber
representing the mean (average value) of values in the column. Note that the given column must containNumber
values››› mean(mytable, "age") 14
median(t :: Table, colname :: String) > Number

Takes a
Table
and aString
representing the name of a column in thatTable
. Returns aNumber
representing the median (middle value) of values in the column. Note that the given column must containNumber
values.››› median(mytable, "age") 13
modes(t :: Table, colname :: String) > List<A>

Takes a
Table
and aString
representing the name of a column in thatTable
. Returns aList<Number>
containing the modes (most frequently occurring values) in the given column, whereA
is the type of data in the given column.››› modes(mytable, "age") [list: ]
››› newtable = table: color row: "blue" row: "blue" row: "red" end ››› modes(newtable, "color") [list: "blue"]
stdev(t :: Table, colname :: String) > Number

Takes a
Table
and aString
representing the name of a column in thatTable
. Returns aNumber
representing the standard deviation (a measure of how spread out values are) of the values in the given column. Note that the given column must containNumber
values.››› stdev(mytable, "age") ~2.160246899469287
count(tab :: Table, colname :: String) > Table

Takes a
Table
and aString
representing the name of a column in thatTable
. Produces aTable
that summarizes how many rows have each value in the given column.››› count(mytable, "age") value count "Eve" 1 "Alice" 1 "Bob" 1
Plots and charts
In this section, we’ll use the following table of heights and
weights, heightweighttable
to illustrate many of the
following plot and chart functions:
heightweighttable = table: height, weight row: 74, 242 row: 69, 162 row: 74, 213 row: 72, 220 row: 70, 206 end
histogram(t :: Table, colname :: String, binwidth :: Number) > Image

Displays an
Image
of a histogram of values in the named column, which must contain numeric data.binwidth
indicates the width of bins in the histogram.››› histogram(heightweighttable, "height", 3)
scatterplot(t :: Table, xs :: String, ys :: String) > Image

Displays an
Image
of a scatter plot from the given table.xs
names the column int
to use for xvalues, andys
names the column int
to use for yvalues. Both columns must containNumber
values.››› scatterplot(heightweighttable, "height", "weight")
lrplot(t :: Table, xs :: String, ys :: String) > Image

Like a call to
scatterplot
with the same inputs. The difference is that a linear regression will be attempted on the elements of the plot, and a regression line will the be drawn over the data.››› lrplot(heightweighttable, "height", "weight")
piechart(t :: Table, ls :: String, vs :: String) > Image

Display an
Image
of a piechart from the givenTable
(one slice per row).ls
is the label to use for the chart, andvs
names the column of theTable
to use for values in the pie chart.examplesyllabustable = table: coursecomponent, gradeweight row: "Drills/Lecture Activities", 10 row: "Labs", 10 row: "Homeworks", 25 row: "Projects", 25 row: "Code Checkins", 15 row: "Exams", 15 end
››› piechart(examplesyllabustable, "coursecomponent", "gradeweight")
barchart(t :: Table, ls :: String, vs :: String) > Image

Displays an
Image
of a barchart from the givenTable
(one bar per row).ls
names the column of theTable
to use for labels, andvs
names the column of theTable
to use for values in the bar chart.examplesyllabustable = table: coursecomponent, gradeweight row: "Drills/Lecture Activities", 10 row: "Labs", 10 row: "Homeworks", 25 row: "Projects", 25 row: "Code Checkins", 15 row: "Exams", 15 end
››› barchart(examplesyllabustable, "coursecomponent", "gradeweight")
freqbarchart(t :: Table, vs :: String) > Image

Display an
Image
of a frequency barchart from the givenTable
. There is one bar for each unique value of the column with namevs
(showing the number of occurrences of that value).favoritecolorstable = table: color row: "blue" row: "red" row: "green" row: "blue" row: "blue" row: "red" end
››› freqbarchart(favoritecolorstable, "color")
boxplot(t :: Table, vs :: String) > Image

Produces an
Image
of a box plot of the values in the column namedvs
in theTable
. A box plot shows the minimum, maximum, and median values of a column, as well as the first (lowest) and third quartiles of the dataset; this is helpful for seeing the variation in a dataset.››› boxplot(heightweighttable, "weight")