Pyret has three different notations for manipulating tables (each makes sense for someone starting tables with a different goal or prior knowledge about programming). The version we are using, based on functions, is not covered in the Pyret documentation or the textbook. This page summarizes the functions you can use to manipulate tables.
Use this, not the Pyret Documentation, for working with tables
filter-by :: (t :: Table, keep :: (Row -> Boolean)) -> Table
Given a table and a predicate on rows, returns a table with only the rows for which the predicate returns true.
sort-by :: (t :: Table, colname :: String, sort-up :: Boolean) -> Table
Given a table and the name of a column in that table, return a table with the same rows but ordered based on the named column. If sort-up is true, the table will be sorted in ascending order, otherwise it will be in descending order.
build-column :: (t :: Table, colname :: String, builder :: (Row -> A)) -> Table
Consumes an existing table, and produces a new table containing an additional column with the given colname, using builder 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 the builder function returns.
add-row :: (t :: Table, r :: Row) -> Table
Consumes a table and a row to add, and produces a new table with the rows from the original table followed by the given row.
add-col :: (t :: Table, colname :: String, c-vals :: List<Any>) -> Table
Consumes a column name and a list of values, and produces a new table with the columns of the original followed by a column with the given name and values. Note that the length of c-vals must equal the length of the table.
select-columns :: (t :: Table, colnames :: List<String>) -> Table
Consumes a table and a list of column names, and produces a new table containing only those columns. The order of the columns is as given in the list.
transform-column :: (t :: Table, colname :: String, f :: (A -> B)) -> Table
Consumes a table, a column name and a transformation function, and produces a new table where the given function has been applied to all values in the named column. The values in the original column are of type A (the input to the function) and values in the new column have type B (the output of the function).
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.row-n(n) -> Row
For a table t, returns the nth row, where row numbers start at 0
t.length() -> Number
For a table t, returns the number of rows in the table
*t.get-column :: (colname :: String) -> List<A>
Returns a list of the values in the named column in table t. A is the type of the data in the named column
sum :: (t :: Table, colname :: String) -> Number
Takes a table and the name of a column in that table. Returns the sum of values in the column. Note that the column must contain numbers.
mean :: (t :: Table, colname :: String) -> Number
Takes a table and the name of a column in that table. Returns the mean (average value) of values in the column. Note that the column must contain numbers.
median :: (t :: Table, colname :: String) -> Number
Takes a table and the name of a column in that table. Returns the median (middle value) of values in the column. Note that the name column must contain numbers.
modes :: (t :: Table, colname :: String) -> List<A>
Takes a table and the name of a column in that table. Returns the modes (most common values) in the column, where A is the type of data in the column.
stdev :: (t :: Table, colname :: String) -> Number
Takes a table and the name of a column in that table. Returns the standard deviation (a measure of how spread out values are) of the values in the column. Note that the column must contain numbers.
count(tab :: Table, colname :: String) -> Table
Produces a table that summarizes how many rows have each value in the named column.
histogram :: (t :: Table, colname :: String, bin-width :: Number) -> Image
Display a histogram of values in the named column, which must contain numeric data. Bin-width indicates the width of bins in the histogram.
scatter-plot :: (t :: Table, xs :: String, ys :: String) -> Image
Display a scatter plot from the given table. xs names the column to use for x values, and ys names the column to use for y values. Both columns must contain numeric data.
lr-plot :: (t :: Table, xs :: String, ys :: String) -> Image
Like a call to scatter-plot 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.
pie-chart :: (t :: Table, ls :: String, vs :: String) -> Image
Display a pie-chart from the given table (one slice per row). ls is the label to use for the chart, and vs names the column of the table to use for values in the pie chart.
bar-chart :: (t :: Table, ls :: String, vs :: String) -> Image
Display a bar-chart from the given table (one bar per row). ls is the label to use for the chart, and vs names the column of the table to use for values in the pie chart.
freq-bar-chart :: (t :: Table, vs :: String) -> Image
Display a frequency bar-chart from the given table. There is one bar for each unique value of vs (showing the number of occurrences of that value).
box-plot :: (t :: Table, vs :: String) -> Image
Produces a box plot of the values in the column named vs in the table. 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.