cs111
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Lab 3: Tables It's time to do our first multi-step data analysis! Everyone knows cows love grass, but did you know that they also love sweets? To help your cow buddies choose the best candies to pick, you've been given access to the results of a survey. [FiveThirtyEight](https://en.wikipedia.org/wiki/FiveThirtyEight) conducted a survey in which tens of thousands of people were asked to choose between two candies; from the survey's responses, they computed the winning percentage of each candy. Then, they compiled a data-set with `Number` attributes like the winning percentage, relative price, and sugar percentage, and also `Boolean` attributes such as chocolate, fruity, caramel, and hard. This data has been published in a FiveThirtyEight article called [“The Ultimate Halloween Candy Power Ranking,"](https://fivethirtyeight.com/features/the-ultimate-halloween-candy-power-ranking/) which is definitely worth a read after the lab. In this lab, you’ll be looking at the **relationships between these columns**. ## Setup First, include this code at the top of your program: ``` provide * provide-types * include tables include shared-gdrive("dcic-2021", "1wyQZj_L0qqV9Ekgr9au6RX2iqt2Ga8Ep") include gdrive-sheets # 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 created by the course staff, so there's no need to create your own data. Please copy the code above and press the "Run" button in the top right corner to process the code in the definitions window. Then type `candy-data` in the interactions window to see the data as a table. You’ll want to refer to the [CS111 Pyret Tables Documentation](https://hackmd.io/@cs111/table) for this lab (*not* the built-in Pyret documentation). You may also want to refer to [our guide to making a plan](https://hackmd.io/@cs111/planning). Suggestion: Use a comment-line as shown below to separate the code for each part. This can help you navigate your code file. ``` #----------------------------------- ``` ## Part 1: Basic Table Filtering ![cow-candy](https://hackmd.io/_uploads/HyAyniUvkg.jpg) Let's use the powers of filtering to learn more from our candy data. Recall the `filter-with` table operation from lecture: it takes an lambda expression that goes from a `Row` to `Boolean` as an input, and uses that expression to determine which rows to put in the output table. **Task 1:** As a warmup to writing our lambda expression, let's write a computation on just one row. Use the code `candy-row = candy-data.row-n(0)` to get the first row of `candy-data`. Write an expression that computes whether the `sugar-percent` of `candy-row` is greater than 0.75. Verify for yourself that the value that this expression evaluates to is reasonable based on the value of the `sugar-percent` cell of `candy-row` by examining `candy-row` in the interaction window. **Task 2:** 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` is greater than 0.75. The lambda expression in your `filter-with` will be your expression from Task 1, rewritten to use `r` instead of `candy-row` (this is because our lambda expression is just a function on every row `r` in our table, so we are going from the specific computation on a specific row in Task 1 to a computation on any input row `r`). [Here](https://hackmd.io/WulrblAaTqCwpZP2C2HzSQ) is a resource on lambda expressions you can use! **Task 3:** 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` is greater than 0.90. **Task 4:** 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 a table (or an expression that evaluates to a table). ## Part 2: Combining multiple table operations Now that you've practiced basic table operations, let's practice computations that involve multiple table operations (or multiple uses of the same table operations). This is also a good opportunity to practice planning programs. ### Practice with Planning The author of the article linked above was interested in combinations of ingredients. Let's say we want to figure out what proportion (percentage) of the candies with chocolate also have caramel. Before we try to *write* the code, let's step back and *plan* what we need to do. We've seen planning (and to-do lists) in lecture. These next tasks are practice with that. **Task 5:** Discuss with your partner what computations you will need for this problem: will you need to filter tables? Build columns? Compute lengths of tables? Create a plan on paper, based on our [How to Create a Plan](/t-b82a5vRM6JmXyDPWV7aQ) document. --- ### *CHECKPOINT:* Call over a TA once you reach this point. ___ **Task 6:** Using your plan as a guide, write an expression (not a function) to compute the proportion of chocolate candies that also have caramel. **Task 7:** Now check for the proportion of chocolate candies that are nutty. Feel free to copy and adapt code that you've already written. How much did you have to change? **Task 8:** Expecting that we'll want to compare several proportions of chocolate candies, let's write a function that generalizes the expressions from the previous two tasks to find the proportion of chocolate candies that have a specific attribute. Write a function `choc-candy-with-attr` that takes a string naming an ingredient. It returns the proportion of chocolate candies that also have the given ingredient. Figure out how to reuse your work on the previous two problems to help with this one! The function declaration should look like this: `choc-candy-with-attr(attr :: String) -> Number:` Experiment with your function in the interactions window (to make sure you know how to use this function). ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Part 3: Adding columns for analysis For our next analysis, let's figure out the highest winning percentage of a candy that is both fruity and hard, but not a pluribus (pluribus means that there are multiple candies in a packet, e.g. Skittles, M&M’s, and Smarties) **Task 9:** Develop a plan for this program on paper. Consider the various table operations we've seen, including `filter-with`, `length`, `build-column`, `order-by`, and `row-n`. *Hint: if you need a piece of information that isn't already in the table, considering computing a new column.* **Task 10:** Extend the candy table with a `Boolean` column that indicates whether a candy is both fruity and hard, but not a pluribus. **Task 11:** Finish executing your plan, ending with an expression that produces the highest winning percentage candy that is fruity, hard, and not a pluribus. **Task 12:** Write another expression to compute the average winning percentage among these fruity-hard-single candies (use the `mean` function in our tables documentation). ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Part 4: Scatterplots and pie charts **Task 13:** Recall from lecture that table columns can contain *categorical*, *numerical*, or *descriptive* data. With your partner, discuss which columns in `candy-data` are categorical and which are numerical. Now, take a look at the [Plots and Charts section](https://hackmd.io/@cs111/table#Plots-and-Charts) of our Tables documentation. Answer the following questions *(Hint: think about how many column inputs each plotting function takes in, and why. Also think about whether these column(s) must be numerical and/or categorical.)* - What is the difference between a freq-bar-chart and a bar chart? - What is the difference between a freq-bar-chart and a histogram? - What is the difference between a pie chart and a bar chart? - What is the difference between a bar chart and a scatter plot? **Task 14:** 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. Look at the scatterplot 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 comment, summarize the relationship in a sentence or two. **Task 15:** Now, go back to the proportions of chocolate questions from Part 2. Using the `pie-chart` function in the tables documentation, generate a chart that shows the proportions of various other ingredients (e.g. fruity, caramel, etc.) in candies with chocolate. ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Part 5: Misleading Data Visualizations **Task 16:** In this lab we have created scatterplots and pie charts that visually represent the data attributes we have been working with. Now, we want to think about how the data could be misconstrued. Below are examples of visual representations of data from [the Economist](https://www.economist.com/), which publishes about 40 charts weekly on their news platforms. If you're interested, read [this article](https://medium.economist.com/mistakes-weve-drawn-a-few-8cdd8a42d368) from a visual data journalist at the Economist reveals that, at times, they get their visual representations of data incorrect. With your lab partner(s), consider how some charts could be misleading and how this could be avoided. Come up with 1-2 reasons explaining why the original chart (labeled “original”) was misleading, and what revisions were done to create a better chart (labeled “better”). ***Please write down your answers and share them with the TA.*** ### 1.Truncating the Scale: ![1_9QE_yL3boSLqopJkSBfL5A](https://hackmd.io/_uploads/SJcLDccs6.png) ### 2.Cherry-picking Scales: ![1_RER4tyXsS086M1Y0K7bz_Q](https://hackmd.io/_uploads/BkPCDccip.png) ### 3.Choosing the visualization method: ![1_9GzHVtm4y_LeVmFCjqV3Ww](https://hackmd.io/_uploads/rktyuc5j6.png) ## Part 6: Comparing Attributes ***This is a discussion and planning question -- you are NOT being asked to write code to do this analysis. In a comment, write a few sentences to summarize your ideas.*** Imagine that we want to understand the relative frequencies of the attributes in the column names. Specifically, we want to be able to choose two attributes (column names) and find out which is more frequent within the entire table. **Task 17:** Discuss with your partner how you might go about testing this function. Write a comment describing your testing plan (what tables would you make, what rows would you need, etc). ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Takeaways This lab has mostly been about getting you comfortable working with tabular data, practicing some common operators on tables, and planning functions. It also gets you thinking about our course's focus on 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. ------ > Brown University CSCI 0111 (Spring 2025) <iframe src="https://forms.gle/ES2gWaUjHqRdypyh8" width="640" height="372" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully