Tibbles and Pipes

Back

Loading concept...

🚂 Tibbles & Pipes: Your Data’s Best Friends

Imagine you’re building with LEGO blocks, but instead of using your hands to pass blocks one by one, you have a magical slide that sends blocks through different stations automatically. That’s what tibbles and pipes do for your data!


🎯 What You’ll Learn

  • Tibble Basics - A smarter way to store data
  • Native Pipe Operator - Connect your work like a train track

📦 Part 1: Tibble Basics

What is a Tibble?

Think of a tibble like a super-organized lunchbox.

A regular data frame is like throwing food in a bag. A tibble is like having a lunchbox with compartments - each snack has its place, and you can see exactly what’s inside without opening everything.

# Old way: data.frame
old_box <- data.frame(
  name = c("Apple", "Banana"),
  count = c(3, 5)
)

# New way: tibble
library(tidyverse)
new_box <- tibble(
  name = c("Apple", "Banana"),
  count = c(3, 5)
)

Why Tibbles Are Special

1. They show you just enough

When you print a tibble, it shows only 10 rows. No more endless scrolling!

print(my_tibble)
# Shows first 10 rows
# Shows column types too!

2. They tell you what’s inside

# A tibble: 2 Ă— 2
#   name   count
#   <chr>  <dbl>
# 1 Apple      3
# 2 Banana     5

See <chr> and <dbl>? That’s the tibble telling you:

  • <chr> = character (words)
  • <dbl> = double (numbers)

3. They never surprise you

Regular data frames sometimes change your text into categories (factors). Tibbles never do this without asking.

Creating Tibbles

Way 1: From scratch

friends <- tibble(
  name = c("Sam", "Alex"),
  age = c(8, 9),
  pet = c("dog", "cat")
)

Way 2: From an old data frame

old_data <- data.frame(x = 1:3)
new_tibble <- as_tibble(old_data)

Way 3: Row by row (tribble)

scores <- tribble(
  ~subject, ~score,
  "Math",   95,
  "Art",    88
)

🌟 Pro Tip: The ~ in tribble marks column names. Think of it as a little flag saying “this is a header!”


đź”— Part 2: Native Pipe Operator

The Magic Slide: |>

Remember our LEGO slide idea? The pipe |> is exactly that!

Without pipes (the old way):

# Step 1: Get data
data1 <- get_data()
# Step 2: Clean it
data2 <- clean(data1)
# Step 3: Sort it
data3 <- sort(data2)
# Step 4: Show it
show(data3)

With pipes (the fun way):

get_data() |>
  clean() |>
  sort() |>
  show()

It reads like a story: “Get data, THEN clean it, THEN sort it, THEN show it.”

How Pipes Work

The pipe takes whatever is on its left and puts it as the FIRST ingredient in the function on its right.

# These are the same:
sqrt(16)       # Result: 4
16 |> sqrt()   # Result: 4
graph TD A["16"] -->|passes to| B["sqrt"] B --> C["4"]

Real Example: Making Breakfast

Imagine making toast:

# Without pipe - hard to read
butter(toast(slice(bread)))

# With pipe - easy to follow!
bread |>
  slice() |>
  toast() |>
  butter()

Native Pipe vs Old Pipe

R has TWO pipes:

Native Pipe Old Pipe
|> %>%
Built into R Needs magrittr
Faster Slightly slower
R 4.1+ only Works everywhere
# Both work the same way:
data |> filter(x > 5)   # Native
data %>% filter(x > 5)  # Old style

💡 Use |> for new projects. It’s built right into R!

Combining Tibbles and Pipes

Here’s where the magic happens:

library(tidyverse)

students <- tibble(
  name = c("Mia", "Leo", "Zoe"),
  score = c(85, 92, 78),
  grade = c("B", "A", "C")
)

# Find top students
students |>
  filter(score > 80) |>
  arrange(desc(score))

# Result shows Mia and Leo,
# sorted by score (Leo first!)

The Placeholder: _

Sometimes you need data in a different spot:

# Normal: data goes first
data |> mean()

# With placeholder: put data
# where you need it
10 |> seq(1, _, by = 2)
# Creates: 1, 3, 5, 7, 9

⚠️ The placeholder _ only works with the native pipe |>, not with %>%.


🎮 Quick Practice

Try these in your R console:

# 1. Create a tibble
pets <- tibble(
  animal = c("dog", "cat", "fish"),
  count = c(2, 1, 5)
)

# 2. Use a pipe
pets |> print()

# 3. Chain multiple steps
pets |>
  filter(count > 1) |>
  arrange(count)

📌 Key Takeaways

  1. Tibbles are better data frames

    • Show types (<chr>, <dbl>)
    • Print only 10 rows
    • Never surprise you
  2. Pipes |> connect your work

    • Read left to right
    • “Then” between each step
    • Makes code easy to read
  3. Together they’re powerful

    • Create data with tibble
    • Transform with pipes
    • Clean, readable code!

🚀 You Did It!

You now know how to:

  • âś… Create tibbles (your smart data boxes)
  • âś… Use pipes (your magical slides)
  • âś… Chain operations together

Next time you work with data, remember: tibbles hold it, pipes move it!

graph TD A["Your Data"] --> B["Tibble"] B --> C["Pipe &#124;&gt;"] C --> D["Filter"] D --> E["Pipe &#124;&gt;"] E --> F["Arrange"] F --> G["Pipe &#124;&gt;"] G --> H["Result!"]

You’re ready to write clean, readable R code. Go build something amazing! 🌟

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.