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"
  • “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))
  • a is a vector of length four
  • log and sqrt are functions that, when passed a vector as input, will return a vectors with the function applied to each element.
  • So they return vectors of length 4 as well.
  • The two functions are nested, so they’ll be evaluated from the inside out

What will happen if you run this code?

a <- 1 + 2
a + 1
  • It will print 4
  • …but the variable 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)
  • “character”
  • the definition of a vector requires every element to be of the same type
  • there’s no way to translate “seven” into 7, so instead 1 and 3.14 must be translated into strings (text blobs)
  • it’s as if you did `a <- c(“1”, “3.14”, “seven”)

Intro to R and RStudio

You already did the Intro to Coding tutorial. Right?

Anyway we’ll go through a bit together, then get to work.

Head to stat20.datahub.berkeley.edu!

R and RStudio

  • R: A free, open-source programming language designed for statistics and data science purposes.
  • RStudio: A software through which you can run R code, compose documents, and easily keep track of your coding session. You can also store and manage your files.

Components of RStudio

  • Console: Where the live R session lives. Type commands into the prompt > and press enter/return to run them. The Console is in the lower-left pane.
  • Environment: The space that keeps track of all of the data and objects that you have created or loaded and have access to. Found in the upper right pane.
  • Editor: Used to compose and edit text (.qmd files) and R code (.r files). Found in the upper left pane.
  • File Directory: Used to navigate between your files/folders on your Rstudio account. Can move, copy, rename, delete, etc. Found in the lower right pane.

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

Object assignment

You can create/save objects using the assignment operator <- . This is the equivalent of = in other programming languages. . . .

my_fav_num <- 11

In order to be recognized as a valid object name, you have to follow certain conventions; namely, the object name should begin with a letter.

good names names that cause errors
a 1trial
b $
FOO ^mean
my_var my var

Functions on vectors

A vector is the simplest structure used in R to store data. It can be created using the function c().

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

A function operates on an R object and produces output. R has many of the mathematical functions that you would expect.

sum(my_vector)
[1] 8

Lab 1

But first: don’t forget your assignments

  • Lab 1 due Tuesday at 8 am on Gradescope.
  • “Worksheet” 0 (just 2 google surveys) due Tuesday at 8 am on Gradescope. 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 (always due the day before the lecture, see Gradescope).
  • Freely ask questions about the content the Taxonomy of Data or Summarizing Categorical Data threads on Ed.

General Lab Workflow

  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. 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).

If you haven’t yet – watch the video on RStudio

It’s right at the top of the course website.

Then start on Lab 1 (link on website).

Animated gif of a shoebill bird.

Break

05:00

Worksheet: Taxonomy of Data

20:00