01 / 21

Tibbles in R
The Modern Data Frame

Learn how tibbles improve on traditional data frames with cleaner printing, stricter subsetting, and safer defaults for modern data analysis.

R Programming

What is a Tibble?

A tibble is a modern reimagining of the classic R data frame. Introduced by the tibble package (part of the tidyverse), tibbles are designed to be lazier and friendlier than traditional data frames.

# Install and load the tibble package install.packages("tibble") library(tibble)

Why Do Tibbles Exist?

Traditional data frames have some surprising behaviors that can introduce subtle bugs. Tibbles fix three major pain points:

1
No partial matching: Data frames let you access df$n even when the column is named name. Tibbles don't.
2
No string-to-factor coercion: By default, older R versions converted strings to factors in data frames. Tibbles never do this, keeping strings as strings.
3
Better printing: Data frames dump all rows to the console. Tibbles print only the first 10 rows.

Tibbles in the Tidyverse

tibble dplyr readr tidyr ggplot2 All tidyverse functions return tibbles

Creating Tibbles

There are two primary ways to create a tibble: the tibble() constructor and converting an existing data frame with as_tibble().

# Method 1: tibble() constructor students <- tibble( name = c("Alice", "Bob", "Carol"), grade = c(92, 85, 97), pass = grade >= 90 ) # Method 2: Convert existing data frame iris_tbl <- as_tibble(iris)

Quick Tables with tribble()

The tribble() function lets you define a tibble row by row: perfect for small, hand-entered data that you want to be readable in your code.

# tribble: transposed tibble grades <- tribble( ~name, ~subject, ~score, "Alice", "Math", 92, "Bob", "English", 85, "Carol", "Science", 97 )

The Printing Difference

This is the most immediately visible benefit of tibbles. Compare what happens when you print a large dataset:

data.frame
Prints all rows and columns. A 10,000-row dataset floods your console and is impossible to read.
tibble
Prints only the first 10 rows and as many columns as fit the screen. Always shows column types.

Tibble Print Output

# Print the students tibble students
# A tibble: 3 x 3 name grade pass <chr> <dbl> <lgl> 1 Alice 92 TRUE 2 Bob 85 FALSE 3 Carol 97 TRUE

Customizing Print Behavior

You can override the default 10-row limit using print() with arguments, or by setting global options:

# Print more rows print(students, n = 20) # Print all rows print(students, n = Inf) # Show all columns (even if they don't fit) print(students, width = Inf) # Set global default to 15 rows options(tibble.print_max = 15)

Stricter Subsetting Rules

Tibbles are more predictable when you extract columns. Here are the two key differences:

No partial matching
If your column is name, typing df$n returns NULL with a warning instead of silently matching.
Consistent [ ] return
Using [ always returns a tibble. Use [[ or $ to extract a single vector.
# Data frame: partial match works (dangerous!) df <- data.frame(name = "Alice") df$n # Returns "Alice" (!) # Tibble: no partial match (safe) tb <- tibble(name = "Alice") tb$n # Returns NULL + warning

data.frame vs tibble

Feature data.frame tibble
Printing All rows/columns First 10 rows + types
String handling Converts to factor (older R) Keeps as character
Partial matching ($) Allowed silently Warning + NULL
[ ] single column Returns a vector Returns a tibble
Column referencing Cannot use earlier cols Can reference earlier cols
Row names Supported Discouraged

How to Start Using Tibbles

1
Load the library: Use library(tibble) or library(tidyverse) at the top of your script.
2
Create with tibble(): Build new tibbles using tibble() instead of data.frame().
3
Convert existing data: Wrap legacy data frames with as_tibble().
4
Use [[ or $ for vectors: Remember that [ returns a tibble, [[ returns a vector.
5
Check with class(): Confirm your object type with class(my_data). Tibbles show "tbl_df", "tbl", "data.frame".

Example: Building a Tibble

# Create a tibble of student test scores scores <- tibble( student = c("Alice", "Bob", "Carol", "Dan"), test1 = c(88, 76, 95, 82), test2 = c(91, 84, 89, 90), avg = (test1 + test2) / 2 ) scores
# A tibble: 4 x 4 student test1 test2 avg <chr> <dbl> <dbl> <dbl> 1 Alice 88 91 89.5 2 Bob 76 84 80 3 Carol 95 89 92 4 Dan 82 90 86

Example: Subsetting a Tibble

# Single bracket: returns a tibble scores["student"] # A tibble: 4 x 1 # student <chr> # 1 Alice # 2 Bob ... # Double bracket: returns a vector scores[["student"]] # [1] "Alice" "Bob" "Carol" "Dan" # Dollar sign: also returns a vector scores$student # [1] "Alice" "Bob" "Carol" "Dan"

Example: Converting and Inspecting

# Convert the built-in mtcars data frame mt <- as_tibble(mtcars) # Check the class class(mt) # [1] "tbl_df" "tbl" "data.frame" # Use glimpse() for a transposed view glimpse(mt)
Rows: 32 Columns: 11 $ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1... $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6... $ disp <dbl> 160, 160, 108, 258, 360, 225... $ hp <dbl> 110, 110, 93, 110, 175, 105...

Common Mistakes

✕ Expecting [ ] to return a vector
Unlike data frames, tibble[, "col"] returns a one-column tibble, not a vector. Use [[ ]] instead.
✕ Relying on partial column names
Code like tb$n to access a column named name will return NULL. Always spell out the full column name.
✕ Forgetting to load the library
Calling tibble() without loading the package gives an error: "could not find function". Load tibble or tidyverse first.

Question 1 of 5

When you print a tibble to the console, what is the default number of rows displayed?

Question 2 of 5

What does tibble[, "col_name"] return, unlike a traditional data frame?

Question 3 of 5

Which function creates a tibble by entering data row by row in a readable format?

Question 4 of 5

What happens when you type my_tibble$n if the only column is named name?

Question 5 of 5

Inside a tibble() call, you can reference a column you just defined on a previous line. What is this feature called?