Primer 2 · Tibbles

The rectangle you actually analyse

ImportantIf the cells below never become ready

This page runs R inside your browser. The first load downloads about 21 MB: R itself, the machinery that runs your code, and the tibble package.

If nothing happens after 2 or 3 minutes, your network is blocking it. Work through the R primer in RStudio instead.

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

Primer 1 left you with vectors. Stand several of them side by side, all the same length, and you have a rectangle.

That rectangle is what you analyse. One row per observation: a person, a household, a notification. One column per variable. In the tidyverse the rectangle is called a tibble.

Run the cells from the top. The first one builds the data that all the others use. If a later cell says object not found, you have skipped it.

Building a tibble

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

Read the header it printed. # A tibble: 5 × 5 means 5 rows by 5 columns.

Under each column name sits its type. <chr> is text. <dbl> is a number with decimals. <lgl> is TRUE/FALSE. A plain data.frame tells you none of that until you ask.

NoteKnow what one row represents

Many “how do I do X in R” problems turn out to be data in the wrong shape.

Say a person appears on 3 rows because they had 3 visits. One row is now a visit, not a person. That is fine, as long as you know it. But if you then count the rows and call it “patients”, you have tripled your sample size without noticing. Before you analyse anything, finish this sentence: “one row is one ____”.

Looking at one

4 functions. Use them on every dataset you open, before anything else.

summary() is the fifth, and it is worth the extra second:

A minimum age of 3, or a blood pressure of 0, shows up here and nowhere else until it has already ruined an analysis.

Getting a column out

$ unwraps one column back into the vector it was.

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

Everything you learned in Primer 1 now applies to a real dataset:

Counting a categorical variable

table() is base R and it is still the fastest way to see what is in a column.

Cross two columns and you have the beginning of a 2×2 table, which is where Primer 7 starts:

Warningtable() drops missing values silently

By default table() does not count NAs and does not tell you it left them out. Your percentages then quietly refer to a smaller denominator than you think. Use table(x, useNA = "ifany") whenever the column might have holes.

What you should now be able to do

  • Build a tibble from named, equal-length vectors.
  • Say what one row of any dataset represents, before you analyse it.
  • Look at a new dataset with nrow, names, str and summary.
  • Pull a column out with $ and use everything from Primer 1 on it.
  • Count categories with table(), and remember that it hides NA by default.
  • See why an overall proportion is not the average of group proportions.

→ Primer 3: packages and the pipe

Back to top