Unit 3:   Manipulating Tables

imageUnit 3Manipulating Tables
Unit Overview

Students extend their knowledge of functions to include methods, and learn about Table methods for sorting, filtering and extending Tables. They are also introduced to Table Plans (a structured approach to manipulating tables), and begin manipulating their own datasets.

English

add translation

Product Outcomes:
    Standards and Evidence Statements:

    Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.

    • Data 3.1.1: Use computers to process information, find patterns, and test hypotheses about digitally processed information to gain insight and knowledge. [P4]

      • Computers are used in an iterative and interactive way when processing digital information to gain insight and knowledge.

      • Digital information can be filtered and cleaned by using computers to process information.

      • Combining data sources, clustering data, and data classification are part of the process of using computers to process information.

      • Insight and knowledge can be obtained from translating and transforming digitally represented information.

    • BS-DR.1: The student is able to translate a word problem into a Contract and Purpose Statement

      • given a word problem, identify the domain and range of a function

      • given a word problem, write a Purpose Statement (i.e. - rewrite the problem in their own words)

    • BS-DR.2: The student can derive test cases for a given contract and purpose statement

      • given a Contract and a Purpose Statement, write multiple examples or test cases

    • BS-DR.4: The student can solve word problems that involve data structures

      • write examples for a function that prodiuces a data structure

      • write functions that produce data structures

    Length: 95 Minutes
    Glossary:
    • function: a mathematical object that consumes inputs and produces an output

    Materials:
      Preparation:

        Types

        Functions

        Values

        Number

        num-sqrt, num-sqr

        4, -1.2. 2/3

        String

        string-repeat, string-contains

        "hello" "91"

        Boolean

        true false

        Image

        triangle, circle, star, rectangle, ellipse, square, text, overlay

        imageimage



        Review

        Overview

        Learning Objectives

          Evidence Statementes

            Product Outcomes

              Materials

                Preparation

                  Review (Time 15 minutes)

                  • ReviewIn the last lesson, you learned how to define functions of your own using the Design Recipe. You defined a number of functions that work on Rows of the animals-table.

                    What are the steps of the Design Recipe?

                  • Open Animals Dataset (w/Functions) in a new tab, select "Save As" from the file menu, and click "Run". As you scroll through the file, you’ll notice functions defined at the top. Some of these functions are similar to the ones you defined earlier, and others are totally new!

                    Open your student workbook to Page 15, and use the code in the Definitions Area to answer the questions there.

                    Note: one of the examples is failing – this is intentional!

                  • The animal shelter might use this dataset in many ways. For example, it might want to see a list of animals ordered from oldest-to-youngest, or a list of names in alphabetical order. The shelter might also want to compute new columns for their dataset. For example, they might want a new, numeric column that shows the animals’ age in weeks instead of years, or make a new Boolean column showing which animals are dogs over the age of 2.

                    Complete to Page 16 in your Student Workbook.

                  Introducing Methods

                  Overview

                  Learning Objectives

                    Evidence Statementes

                      Product Outcomes

                        Materials

                          Preparation

                            Introducing Methods (Time 15 minutes)

                            • Introducing MethodsYou’ve been calling functions like triangle, num-sqr or num-min for a while now, and the rules are pretty straightforward: at any point, you can just write the name of the function, then pass in one or more arguments inside parentheses. num-sqr(4) will always evaluate to 16, because the function always works the same way and 4 squared is always 16.

                            • Pyret also has another kind of procedure, which behaves a little differently. These procedures are called table methods. Let’s take a look at one table method, and compare it to a function you already know.

                              Type both of these into the Interactions Area and see what comes out.   Both of these expressions do the same thing, but they are written quite differently. How many differences can you find?

                              Have students volunteer as many as they can.

                            • Table methods are different from functions in three important ways:

                              • They are called differently. A method has to be called as part of a Table, and cannot be used on its own. You can write get-row wherever you like, but .row-n only works when attached to the name of a Table, separated by a dot.

                              • Their contracts are different. The .row-n method only exists within the Table Type, so we can’t use its name without also specifying the name of a Table. The contract for a method includes the Type along with the name:  

                              • They have a "secret" argument. The following code uses methods and functions to extract the first row of a table. The function takes in two arguments, but the method appears to only take in one. Can you see the secret argument?  

                              This method v. function distinction is subtle, and it’s worth spending some time walking through it carefully.

                            • How well do you understand methods? Complete Page 17 in your Student Workbook.

                              Have students discuss their answers to Question 7 - for the expressions that don’t work, can they explain why?

                            Ordering Rows

                            Overview

                            Learning Objectives

                              Evidence Statementes

                                Product Outcomes

                                  Materials

                                    Preparation

                                      Ordering Rows (Time 10 minutes)

                                      • Ordering RowsPyret lets you change the order of a table’s rows with the order-by method. Let’s take a look at the contract for this method, and an example of that orders the animals-table by species:   You can find the contract for this method written at the back of your Student Workbook, along with all the other contracts.

                                        Type the animals-table.order-by example into the Interactions Area. What did you get? What will happen if you change true to false? How could you sort the table alphabetically by pet name? In order of oldest-to-youngest?

                                        Pyret has a way to sort by multiple columns as well, but that requires one additional concept that students haven’t seen yet. Documentation can be found here.

                                      • In the Interactions Area, use the .order-by method to produce a table with all the animals sorted alphabetically by name.

                                      Filtering Rows

                                      Overview

                                      Learning Objectives

                                        Evidence Statementes

                                          Product Outcomes

                                            Materials

                                              Preparation

                                                Filtering Rows (Time 10 minutes)

                                                • Filtering RowsSometimes we want to create a subset of our data. For example, we might want to filter the rows so that we get a table of only the lizards at the shelter! When we want to filter a Table, we can use the .filter method. The contract for this method is shown below, along with an example expression that filters the animals-table to show only those who are fixed.  

                                                • Notice that the Domain for the filter method takes in a single value (test), but that test is also a function! Pyret lets us pass functions into other functions, just as easily as we pass Numbers, Strings, Booleans or Images.
                                                  • According to the contract for .filter, what datatype does the test function consume?

                                                  • What datatype does the test function produce?

                                                  Proceed with caution here: do a lot of checking for understanding!

                                                • In the Interactions Area, use the .filter method to produce a table of all the kittens.

                                                Building Columns

                                                Overview

                                                Learning Objectives

                                                  Evidence Statementes

                                                    Product Outcomes

                                                      Materials

                                                        Preparation

                                                          Building Columns (Time 10 minutes)

                                                          • Building ColumnsSometimes we want to add a column to a Table, and we can use the .build-column method to do just that. The contract for this method is shown below, along with an example expression that adds a birth-year to the animals-table.  

                                                          • In the Interactions Area, use the .build-column method to produce a table that includes a nametag column, which contains an image of the nametag for each pet.

                                                          Table Plans

                                                          Overview

                                                          Learning Objectives

                                                            Evidence Statementes

                                                              Product Outcomes

                                                                Materials

                                                                  Preparation

                                                                  Table Plans (Time 30 minutes)

                                                                  • Table PlansTable methods can be chained together, so that we can build, filter and order a Table. For example:   This code takes the animals-table, and builds a new column. According to our Contracts Page, .build-column produces a new Table, and that’s the Table whose .filter method we use. That method produces yet another Table, and we call that Table’s order-by method. The Table that comes back from that is our final result.

                                                                    Suggestion: use different color markers to draw nested boxes around each part of the expression, showing where each Table came from.

                                                                  • It can be difficult to read code that has lots of method calls chained together, so we can break them up before each "."" to make it more readable. Here’s the exact same code, written with line breaks:  

                                                                  • These table methods are powerful, and there’s an order-of-operations to how they are used. For example, we might want to build a column and then use it to filter or order the table. Therefore, .build-column always has to come first! To help keep things organized, we can use Table Plans. Turn to page Page 18

                                                                    Table Plans are like the Design Recipe, but for manipulating tables. They enforce a way of thinking, which is important for your students.

                                                                  • Table Plans are a lot like the Design Recipe. They start with a Contract and Purpose Statement, but involve different kinds of examples and can often involve multiple function definitions. Let’s do an example, which ties together all the pieces you’ve seen before. Suppose it’s time to vaccinate all kittens, and the shelter wants a table that includes nametags for all the kittens in alphabetical order. How do we get started?

                                                                    Your students should be very comfortable with the Design Recipe before proceeding!

                                                                  • We’re going to build a function that does this for us, and we’ll start with the name. Naming is more complex in Table Plans, since we want to name the function according to the most important parts of what it does. Since we’re getting a table of kittens with nametags, we’ll call it get-kittens-tags. Instead of consuming Rows, this time we’re consuming and producing Tables. This gives us the following:

                                                                     

                                                                    Ask students to volunteer other names - but push them to keep the get- part of the name!

                                                                  • This is really similar to writing examples with the Design Recipe, but everything stays on paper. First, we write down a small sample of the animals-table, called a Sample Table. Then we write a sample result, which we’d get from calling get-kittens-tags on that Sample Table. For now, we’ve provided the Starter and Results for you.

                                                                    • Does the Result have our new column?

                                                                    • Does the Result have only kittens?

                                                                    • Is the Result in alphabetical order?

                                                                  • The final step is to define a function that executes our Table Plan. We already know how to start:  

                                                                    Assessment opportunity: students should be able to write this code this based on what they know about the Design Recipe.

                                                                  • Table plans include two parts: defining our new table, and producing our result. To define this table, we’ll ask ourselves four questions, in order:

                                                                    • Does our Result have more columns than our Sample Table? If so, we’ll need to use .build-column.

                                                                    • Does our Result have fewer rows than our Sample Table? If so, we’ll need to use .filter.

                                                                    • Does our Result have its rows in some order? If so, we’ll need to use .order-by.

                                                                    If the answer to any of these questions is "no", cross out that line in the template.

                                                                    All three are needed.

                                                                  • All three methods are needed, so we won’t cross anything out. You’re already familiar with definitions in Pyret, and that’s what we’ll use here. Let’s start with the name t for Table.  

                                                                    It may be helpful to start with all of these methods on one line, and have students see you break them up. Students should be reminded that both forms are valid, but encouraged to use the latter.

                                                                  • Now we can fill in those ... sections!

                                                                    • For each Row, we know we need to build a column for the nametags: what should that column be called? Do we have a function that takes a Row and produces a nametag?

                                                                    • We know we need to filter so that all of our Rows are kittens: Do we have a function that takes a Row and tells us whether or not it is a kitten?

                                                                    • We know we need to order these rows: by what column should we order them? Ascending?

                                                                  • Filling in these blanks, we get the following code:   Fortunately, the nametags and is-kitten functions are already defined! If we found that we needed to make news ones, however, we could use the Design Recipe to do it.

                                                                    If projecting onto a board, drawing arrows from the function names to their definitions would be really helpful here.

                                                                  • The final step in the Table Plan is to produce the result. For now, that result is just the table we defined, t! (Don’t worry, you’ll get to more complex results later)  

                                                                    Drawing arrows from the t expression on the last line back to the t definition on the first line would be a good idea here. Make sure students see the connection between "defining the table...and using it"!

                                                                  • Once you’ve typed in the Contract, Purpose and Function Definition, click "Run". How do we use this function? If you look in the Examples section, you’ll see that the Result is written underneath the expression get-kittens-tags(animals-table). That’s the code that should give us the result, so let’s type it in!

                                                                    Type in the code and hit Enter. Did you get back the same result you expected?

                                                                    • Turn to Page 19, and complete the Table Plan. When your teacher has checked your work, type in your code to create the table. Note: this time, you’ll need to fill in more missing parts of the function definition!

                                                                    • Turn to Page 20, and complete the Table Plan. When your teacher has checked your work, type in your code to create the table. Note: this time, you’ll need to come up with your own Contract and Purpose!

                                                                    • Turn to Page 21, and complete the Table Plan. When your teacher has checked your work, type in your code to create the table.

                                                                  • By now, you’ve had a chance to use Table Plans and Table Methods to:

                                                                    • Extend data with computed columns

                                                                    • Filter rows to create subsets of data (e.g. "only cats")

                                                                    • Order rows by a particular column, in ascending or descending order

                                                                    Turn to Page 22 in your Student Workbook, and fill out questions 3-5.

                                                                  Closing

                                                                  Overview

                                                                  Learning Objectives

                                                                    Evidence Statementes

                                                                      Product Outcomes

                                                                        Materials

                                                                          Preparation

                                                                          Closing (Time 5 minutes)

                                                                          • Closing Congratulations! You’ve just learned the basics of the Pyret programming language, and how to use that language to answer a data science question. Throughout this course, you’ll learn new and more powerful tools that will allow you to answer more complex questions, and in greater detail.

                                                                            Make sure to save your work. Hit the Save button in the top left. This will save your program in the code.pyret.org folder within your Google Drive.

                                                                            If your students are working in pairs/groups, make sure that each student has access to a version of the program. The student who saved the program to their Google Drive can share their program with anyone by hitting the Publish button in the top left, choosing "Publish a new copy", then clicking the "Share Link" option. This will allow them to copy a link to the program, then send to their partners in an email/message.