Taxonomy of Data

STAT 20: Introduction to Probability and Statistics

Warmup Questions

What type of variable is “age”?

Classify the recorded variable as continuous numerical, discrete numerical, nominal categorical, or ordinal categorical.

What type of variable is “age” here?

  • Age ranges of television audiences/demographics
  • Categorical ordinal
  • They aren’t quite numbers (18-24, 25-34, etc) - they are ranges, and you can’t really add or subtract two age ranges to get a meaningful result.

What type of variable is “age” here?

  • Ages of UC Berkeley students
  • Numerical discrete
  • Technically your “age” could be how many microseconds since you were born, but usually when we say “age” we mean “complete revolutions around the sun” which is discrete.

What type of variable is “age” here?

  • The age of a lightbulb (time until it stops working)
  • Numerical continuous

Basic coding

Make your best educated guess about what the following blocks of code will do when run.

What will happen if you run this code?

1 + "one"
  • Error
  • “one” (in quotes) is just seen as a blob of text. It’s not “interpreted” any further.
  • R doesn’t know how to add a number to a blob of text. So it throws an error.

What will happen if you run this code?

a <- c(1, 2, 3, 4)
sqrt(log(a))
  • The two functions are nested, so they’ll be run from the inside out
  • log is run first, taking the natural log of each element
  • sqrt will then act on each element.
  • The result is still 4 numbers:
    • 0.0000000 0.8325546 1.0481471 1.1774100

What will happen if you run this code?

a <- 1 + 2
a + 1
  • It will print 4
  • …but a will still be equal to 3.
  • To change a requires the assignment operator. Something like: a <- a + 1

What will happen if you run this code?

a <- c(1, 3.14, "seven")
class(a)
  • It will print “character”
  • Vectors require every element to be of the same type
  • So far, they aren’t the same type, so R tries to convert them to some common type.
  • It doesn’t know how to convert “seven” into a number, so instead 1 and 3.14 must be converted into text.
  • It’s as if you did `a <- c(“1”, “3.14”, “seven”)

Quick Review: R and RStudio

Before class, you should have already done today’s tutorial (“Intro to Coding”)

…if not, do that first.

We’ll go through a bit of it together, then get to work.

Head to stat20.datahub.berkeley.edu!

R and RStudio

  • R: A language that you can use to tell a computer what to do.
    • R is free, built with statistics in mind, and open-source (anyone can see how it works).
  • RStudio: Literally like a studio space where you do your work.
    • A place to write and run R code, track files, render pretty documents, etc.

Components of RStudio

  • Editor (upper left pane): Where we do most of our work in this class. In “quarto” files that let you mix text and code.
  • Console (lower left): A different place where you can send individual R commands to the computer.
  • Environment (upper right): A view into the computer’s “mind” - information on all the variables it knows about right now.
  • File Directory (lower right): Just like a file directory on your regular computer. You can copy, delete, upload, download, make sub-folders, etc. The usual.

R as a calculator

R allows all of the standard arithmetic operations.

Addition

1 + 2
[1] 3

Subtraction

1 - 2
[1] -1

Multiplication

1 * 2 
[1] 2

Division

1 / 2
[1] 0.5

R as a calculator, cont.

R allows all of the standard arithmetic operations.

Exponents

2 ^ 3
[1] 8

Parentheses for Order of Ops.

2 ^ 3 + 1
[1] 9
2 ^ (3 + 1)
[1] 16

Creating variables


To hold on to a number, some text, or anything else, store it in a variable.

price <- 11


You can change the meaning of a variable any time

price <- 7
price <- price + 3
price
[1] 10

Naming variables

Variable names have restrictions. Roughly:

  • Start with a letter
  • Use only letters, numbers, and under_scores
  • No spaces or other funny business
  • No using names that already have meaning to R (like sum or c).
OK Not OK
sf49ers 49ers
omgBEARS cash$$$
my_var my var

Functions

Functions are specially-named things that take inputs (“arguments”) and give an output (“return value”).

R comes with some built-in functions for you:


sum adds up some numbers:

sum(1, 2, 3, 4)
[1] 10


log10 takes the log base 10 of a number

log10(1000)
[1] 3

Vectors

A vector is basically a box of stuff.

Make a vector with the c function

my_vector <- c(1, 3, 4)
my_vector
[1] 1 3 4

Remember, all the “stuff” has to be the same kind of stuff (all numbers, all text, etc)

Functions on vectors

Some functions expect a vector as input.

mean(my_vector)
[1] 2.666667


Other functions can take a vector, but will do their thing on each element:

sqrt(25)
[1] 5
nums <- c(1, 9, 25, 100)
sqrt(nums)
[1]  1  3  5 10

Worksheet: Taxonomy of Data

If you finish early, start looking at the lab (on the website)

20:00

Your First Lab

Aside: don’t forget your assignments!

  • See Gradescope, it lists everything
  • Lab 1 due Tuesday at 8 am
    • Again, ALL assignments are submitted on Gradescope.
  • Scan your worksheets to pdfs (ask a tutor how), and hold on to those scans for now.
  • “Class Surveys” (just 2 google surveys) due Tuesday at 8 am.
    • The links to the surveys are on the gradescope assignment itself.
  • You’ll fall behind if you didn’t read the notes and tutorial for Taxonomy of Data (today’s content).
  • Start reading the notes for Summarizing Categorical Data (Wednesday’s content).
    • Complete your reading questions (RQs) - they are quick.
    • RQs are always due the day before the lecture (so Thursday midnight is the deadline for the first one).
  • Freely ask questions about the content the Taxonomy of Data or Summarizing Categorical Data threads on Ed.

General Lab Workflow

(All of this is covered in the video on the homepage)

  1. Lab instructions are posted on the course website.
  2. You’ll click the (R) link in the upper right of the website to work in R Studio, which is running on a cloud computer for you.
  3. You’ll write a “Quarto” document that blends text and R code.
  4. When finished, you’ll “render” it to create a pdf, then download it.
  5. Go to Gradescope, find the assignment, and upload that pdf.
  6. Be sure to “assign pages” – it helps us grade faster (so you get grades back faster).

Start Lab 1 (rest of class)

  • First watch the video on RStudio if you haven’t yet.
  • (It’s right at the top of the course website.)
  • Then get cracking on the lab.

Animated gif of a shoebill bird.