Primer 2 · Tibbles: the data rectangle

Rows are observations, columns are variables

ImportantIf the cells below never become ready

This page runs R inside your browser. It needs about 21 MB on first load (the R engine, the machinery that runs and marks your code, and the tibble package) and a minute or two on a slow connection. If nothing happens after that, your network is probably blocking it. Work through the R primer in RStudio instead. Everything taught here is taught there too.

There is no Stop button. If you write a loop that never ends, reload the page.

Primer 1 left you with vectors. Stand several equal-length vectors side by side and you have the rectangle you will actually analyse: one row per observation (a student, a patient, a session) and one column per variable. In the tidyverse that rectangle is a tibble. For the full explanation, see the R primer.

Building a tibble

tibble() takes named vectors and lines them up as columns. Each one must be the same length.

Read the header it prints. # A tibble: 4 × 4 is four rows by four columns, and under each column name sits its type: <chr> for text, <dbl> for a number with decimals, <lgl> for TRUE/FALSE. A plain data.frame tells you none of that until you ask.

Note that batch is <chr>, not a number. Years used as labels are text. You would never take their mean.

Looking at one

Four questions cover almost everything you will want to know about a table you have just been handed.

glimpse() is the function you will reach for when something goes wrong. It turns the table on its side so every column gets one line (name, type, first few values) however wide the data. Nine times out of ten the problem is that a column is text when you thought it was a number, and glimpse() shows you that in one look: $ attendance <dbl> is a number you can average, $ attendance <chr> is not.

Pulling out a column

$ takes one column out of the rectangle and hands it back as a plain vector, exactly the kind of thing Primer 1 taught you to work with.

That is the whole pattern: a tibble is a bundle of vectors, and $ unwraps one.

NoteExercise 1: build a tibble

Time: 5 minutes

Three viva stations were marked out of 10: History scored 8, Exam scored 7 and Counselling scored 9. Put them into a tibble called viva with two columns, station and marks.

viva <- tibble( station = c("History", "Exam", "Counselling"), marks = c(8, 7, 9) ) viva
viva <- tibble(
  station = c("History", "Exam", "Counselling"),
  marks   = c(8, 7, 9)
)

viva

Each column is a vector, so each one needs its own c(). The station names are text and go in quotation marks; the marks are numbers and do not.

NoteExercise 2: average the attendance

Time: 5 minutes

Using the students tibble from the top of the page, work out the mean attendance across the four students. Pull the column out with $, then take its mean.

students <- tibble( student_id = c("S01", "S02", "S03", "S04"), batch = c("2023", "2023", "2024", "2024"), attendance = c(88, 72, 95, 64), passed = c(TRUE, TRUE, TRUE, FALSE) ) mean(students$attendance)
students <- tibble(
  student_id = c("S01", "S02", "S03", "S04"),
  batch      = c("2023", "2023", "2024", "2024"),
  attendance = c(88, 72, 95, 64),
  passed     = c(TRUE, TRUE, TRUE, FALSE)
)

mean(students$attendance)

students$attendance gives you a vector of four numbers. mean() takes it from there.

Check yourself

NoteFour questions on the Moodle course

Take Check yourself · Primer 2 on the Moodle course. Four questions, unlimited attempts, no marks. Every answer comes back with an explanation, so a wrong one costs you nothing and teaches you something.

Next

→ Primer 3 · Packages and the pipe: how to load the tools, and how to chain them together.

Back to top