By the end of this lesson you will be able to
- use the statistics workbench: type commands and press Run
- load a dataset and give it a name
- describe a table in terms of rows (the things measured) and columns (what was measured)
- pick out one column, count the rows, and find the smallest and largest values
- read the workbench's messages when something goes wrong
Every grey box on this page with a Run button is a live statistics workbench that runs inside your browser. You can change the commands in it and press Run (or Ctrl + Enter) as often as you like; Reset puts the original commands back. In the exercises, type your answer, press Check my answer, and the checker tells you whether it is right. Show solution is there if you get stuck.
Emily meets the records
Emily Hartwell has just started work at Brookfield Growers, a co-operative of fruit and vegetable farms that sells to shops and runs a small farm shop of its own. On her first morning, Daniel Mercer, who keeps the co-operative's records, sits down beside her.
“We weigh things, count things and ask people things all day long,” he says. “Apples, sacks of potatoes, deliveries, customers. All of it ends up as tables of numbers. My job, and soon yours, is to turn those tables into answers. Let's start by simply looking.”
Your first command
The workbench understands short commands, one on each line. It works through them from the top, and shows the result of each one. The simplest command is a sum, just as on a calculator:
Press Run. Each command appears after a › sign, with its answer underneath. The workbench uses * for multiplying and / for dividing, and brackets work as they do in arithmetic lessons: the sum inside them is worked out first. Try changing the numbers and running it again.
Loading a dataset
The co-operative's records are stored as datasets. To use one, you load it with data(...) and give it a name, so that you can refer to it in later commands:
› apples = data("apples") ✓ apples is now a table of 120 rows and 6 columns
Read the line from left to right: “let apples be the dataset called apples”. The = sign gives a name to a value. The name goes on the left and the value on the right. The dataset's name goes in quotation marks, because it is a piece of text rather than a name you have made. Once a table has a name, head(...) shows its first few rows:
Rows and columns
Almost every dataset is a table like this one, and statisticians describe tables in the same way:
- Each row is one thing that was measured or observed: here, one apple. The rows are sometimes called cases or observations.
- Each column is one thing that was recorded about every row: here the apple's number, its variety, its weight in grams, its diameter in millimetres, the block of the orchard it grew in, and the day it was picked. The columns are sometimes called variables, because their values vary from row to row.
So the first row says: apple number 1 is a Russet, weighs 151 grams, is 69.1 millimetres across, grew in the south block and was picked on day 1. Before working out anything at all, it is worth asking of any table: what is one row? and what does each column mean, and in what units? Many mistakes in statistics start with a misunderstood table.
Three commands tell you about the shape of a table:
| Command | What it gives |
|---|---|
head(apples) | the first 6 rows (head(apples, 10) gives the first 10) |
count(apples) | how many rows there are |
columns(apples) | the names of the columns |
datasets() lists every dataset you can load in this course, with a short description of each. You will meet most of them in the coming lessons.
One column at a time
To work with a single column, write the table's name, a full stop, and the column's name, with no spaces: apples.weight means “the weight column of the apples table”. It gives a list of 120 numbers, one for each apple. The workbench shows the first 30 and tells you how many there are in all.
Most statistics commands take a column like this inside their brackets. Two of the simplest are min(...), the smallest value, and max(...), the largest:
› max(apples.weight) 238 › min(apples.weight) 100
The heaviest apple weighed 238 grams and the lightest 100 grams. sort(...) puts a list in order, smallest first, or largest first if you add descending = true:
Look at the start of the sorted list: 238, then 194, 193, 192. The heaviest apple is more than 40 grams heavier than any other. Daniel raises an eyebrow at that. It might be a genuine giant, or someone might have weighed it on a tray. Noticing values like this one, which statisticians call outliers, is one of the first things to do with any data. Lesson 2.3 comes back to it.
You can also type a list of your own, inside square brackets with commas between the values, and give it a name:
› boxes = [12, 9, 15, 11] ✓ boxes is now a list of 4 values › sum(boxes) 47
Common mistake: small slips in names
The workbench is exact about names. Apples and apples are different names, a missing full stop or bracket stops a command, and a misspelt column is not guessed at. When something is wrong, the workbench stops at that line and says what it did not understand, often with a suggestion:
› max(apples.wieght)
Line 2: the table has no column called wieght. Did you mean weight? Its columns are: id, variety, weight, diameter, block, day.
Read the message, fix the line it names, and press Run again. Every line above the mistake has still been run, so you can see their results.
A line that starts with # is a comment: the workbench ignores it. Comments are useful for notes to yourself, such as # weights are in grams.
Exercises
Each exercise has its own workbench. Type your commands, then press Check my answer.
Exercise 1 · Load the survey
The farm shop asked its customers some questions. Load the dataset called survey, give it the name survey, and show its first 6 rows with head(...).
Exercise 2 · How many customers?
The survey has been loaded for you. Use a command to show how many customers (rows) took part.
Exercise 3 · The busiest customer
The visits column says how many times each customer came to the farm shop last month. Show the largest number of visits made by any customer.
Exercise 4 · Lightest and heaviest
Load the sacks dataset (sacks of potatoes and their weights in kilograms) under the name sacks. Then show both the smallest and the largest weight, each on its own line.
Exercise 5 · Your own list
On Monday the farm shop sold these numbers of jars of honey on each of its five opening hours: 4, 9, 6, 2, 7. Make a list of them called jars, then show the total number of jars sold with sum(...).
Quick check
Four questions to confirm the main ideas. Choose an answer to see the explanation.
In the apples table, what is one row?
Each row is one apple, and each column is something recorded about every apple.
What does apples.weight give?
The full stop picks one column of the table: a list of 120 weights. Commands such as max(...) then work on that list.
What does the line sacks = data("sacks") do?
A single = gives the name on its left to the value on its right. It shows only a short confirmation, not the table.
Why is it worth sorting a column before doing anything else with it?
Sorting changes nothing about the data itself, but a value far from all the others, like the 238-gram apple, stands out at once.
Summary
- The workbench runs one command per line, from the top, and shows each result.
name = data("dataset")loads a dataset and gives it a name; a single=always gives a name to a value.- In a table each row is one thing observed and each column is one thing recorded about it; always ask what a row is and what the units are.
head,countandcolumnsdescribe a table;table.columnpicks out one column as a list.min,max,sortandsumwork on lists;[4, 9, 6]makes a list of your own.
Found a mistake on this page, or something unclear? Report a problem and mention “Statistics Lesson 1.2”.
