Lab 3: Tables

As you’re packing your bags and getting ready leave the comforts of 221B Baker Street (Sherlock Holmes’s flat), you catch a glimpse of a band of crooks running out of your favorite bakery. You dash into the bakery, but you’re too late! The baker tells you that the crooks have stolen the secret formula to the bakery’s famous candies. Now that the shop no longer has the formula, the baker can only give you the data sets that they used to create this candy in order to help you solve the case.

Setup

In this lab you’re going to be working with tables!

This data has been published in a FiveThirtyEight article called “The Ultimate Halloween Candy Power,” which is definitely worth a read after the lab. FiveThirtyEight conducted a survey in which tens of thousands of people were asked to choose between two candies; they then computed the winning percentage of each candy. The data-set gives a Number with this winning percentage, and also Booleans about the attributes of the candies and Numbers about the relative price and sugar of the candies.

You’ll be looking at the relationships between these columns in this lab.

First, include this at the top of your program:

include tables
include shared-gdrive("cs111-2019.arr", "1PzXKPvJHTi3N_QTShsALKgYaV77ybeqx")
include gdrive-sheets
include image

# spreadsheet id from Google Sheets
ssid = "1XzeWZToT-lqPFVpp-RZLimdoGdGaTcVQZ_8VbDbAkOc"
data-sheet = load-spreadsheet(ssid)
candy-data = 
  load-table: name, chocolate, fruity, caramel, nutty, nougat, crisped-rice, 
    hard, bar, pluribus, sugar-percent, price-percent, win-percent
    source: data-sheet.sheet-by-name("candy-data", true)
  end

This code loads a Pyret table from a Google Sheet. Type “candy-data” in the interactions window to see the data.

You’ll want to refer to the CS-111 Pyret Tables Documentation for this lab (not the built-in documentation).

Part 1: Building columns and analyzing them

1.1: New column

Build a column with Boolean values that indicates whether a candy is fruity and hard, but not a pluribus. Write an expression in the definitions window that uses this new column to compute how many candies meet this condition.

(Pluribus means that there are multiple candies in a packet, ex: Skittles, M&M’s, and Smarties)

Hint: Take a look at the build-column function. Call over a TA if you want help using this.

1.2: Maximum

Of the candies for which this Boolean is true (fruity and hard, but not a pluribus), which has the highest winning percentage?

Hint: This requires the use of the sort-by function, in addition to the row-n method.

An example of the row-n method to take the fifth row of a table: table.row-n(4).

1.3: Mean

Of the candies for which this Boolean is true, what’s the average winning percentage?

Hint: This requires the use of the mean function.

1.4: Median

Of the candies for which this Boolean is true, what’s the median winning percentage?

Hint: This requires the use of the median function.


CHECKPOINT: Call over a TA once you reach this point.


Part 2: Filtering

Now we want to use the powers of filtering to learn more from our candy data.

2.1: Sugar Rush

We want to know which candies have the most sugar. Write an expression in the definitions window that produces a table containing only the candies where sugar-percent > 0.75.

Hint: Take a look at the filter-by function, and remember that functions can be passed as inputs to other functions. Call over a TA if you want help using this structure.

When you’re done writing the expression, copy and paste
#----------------------------------- into the definitions window directly below it to separate it from the next part.

2.2: Bougie

Now that we know how to satisfy our sweet tooth. Write an expression in the definitions window that produces a table containing only the candies where price-percent > 0.90.

Once again, separate it from the next part by pasting
#----------------------------------- on the line below it.

2.3: Chocolate

How many of the candies have chocolate?

Write an expression in the definitions window that outputs this number in the interactions window. You can get the length of a table by writing TABLE.length() where TABLE is the name of (or expression that computes) a table.

Once again, separate it from the next part by pasting
#----------------------------------- on the line below it.

2.4: Chocolate and caramel

Of the candies that have chocolate, what proportion also have caramel?
Write an expression in the definitions window that outputs this proportion in the interactions window.

Once again, separate it from the next part by pasting
#----------------------------------- on the line below it.

2.5: Chocolate and nutty

Of the candies that have chocolate, what proportion are also nutty?

Write an expression in the definitions window that outputs this proportion in the interactions window.

Once again, separate it from the next part by pasting
#----------------------------------- on the line below it.


CHECKPOINT: Call over a TA once you reach this point.


2.6: Chocolate and anything

What attribute is paired most frequently with chocolate?

To help answer this question, write a function that generalizes the expressions from 2.4 and 2.5. This function will take in a String representing the name of the ingedient being paired with chocolate. As we did in week 1, look for commonalities across the code you wrote for caramel (2.4) and nutty (2.5).

Compare the results of your function on the inputs “fruity,” “nutty,” and “caramel.” You don’t have to write code for this comparison – just use your new function to compute all three proportions and compare them manually.


CHECKPOINT: Call over a TA once you reach this point.


3: Scatterplot

What’s the relationship between sugar and winning percentage? Do these two attributes seem correlated? One way to gain intuition on this is to create a scatterplot that puts one attribute on each axis.

The tables documentation includes a scatter-plot function. Look at the documentation and try to figure out how to use it to generate a scatterplot of sugar versus winning percentage (it does not matter which variable goes on each axis). Write an expression in the definitions window that creates the scatterplot. In a sentence or two, summarize the relationship in a comment.

Hint: Check the tables documentation, linked above, for how to do this.

4: Comparing Attributes

Candy analysts want to understand the relative frequencies of the attributes in the table across the collection of candies studied. They want a function where they can give you names of two attributes (column names in the dataset), and you tell them which is more frequent (your function can return “same” if the two attributes are in the same number of candies). Design this function.

Discuss with your partner how you might go about testing this function. Write a comment describing your testing plan.

Now imagine that the analysts want to see the relative frequencies across all of the attributes. How would you want to report that information, and what would be needed to produce that report from the given dataset? You can think about the functions in the documentation, or you can think about reorganizing the data in the table to make it easier.

This is a discuss-with-partner question — you are NOT being asked to write code to do this analysis. Write a few sentences to summarize your ideas in a comment.

5: Exploration (if you have time)

Now think about questions you have about this data, or come up with interesting questions to ask. Investigate (by writing and running expressions), and write a couple sentences in a comment about what you found.

Takeaways

This lab has mostly been about getting you comfortable working with tabular data and practicing some common operators on tables. It also gets you thinking ahead a bit to our course theme of data: what patterns of manipulating data do we often use in computations? How does the organization of our data impact our ability to answer these questions?

Here, we see that filtering, ordering, and summarizing data are some of the key operations. So far we’ve only looked at these operations on tabular data, but these same building blocks will arise many times through this course. When you have a computation to perform around data, you should start by thinking through what combinations of filtering, sorting and summarizing will help you compute your answer.

Another case cracked

Great job! With your help, Sherlock was able to find the missing recipie and your favorite bakery can start making their delicious goodies again. To thank you and Sherlock, the baker has also given you some decadent chocolate truffles.