R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
49: tibble Package In Depth
I've seen this exact scenario play out in code reviews more times than I can count. A developer spends weeks writing a script using base R data.frame objects, then decides to "modernize" the code by switching to tibble, and suddenly, their data processing pipeline just... stops working. They aren't getting a loud crash, but their calculations are returning NA or weird errors.
library(tibble)
# A simple dataset of sensor readings
readings <- tibble(
sensor_id = 1:3,
value = c(23.5, 25.2, 22.8)
)
# This function was written for base R data.frames
calculate_avg <- function(df) {
# The dev wants the mean of the second column (value)
mean(df[, 2])
}
calculate_avg(readings)
# Result: Warning: argument is not numeric or logical; returning NA
The Single-Column Subset Trap
If readings had been a standard data.frame, df[, 2] would have "dropped" the dimensions and returned a simple numeric vector. mean() loves vectors. But a tibble is designed to be consistent. When you subset a tibble with [ , ], it always returns another tibble, even if there is only one column left.
You're essentially trying to calculate the mean of a table, not the mean of the numbers inside the table. To fix this, you need to be explicit about wanting a vector. I usually recommend pull() from the dplyr ecosystem, but if you want to stay within the tibble mindset, use double brackets [[ ]].
# Fix 1: Using double brackets (the base R way that works for tibbles)
calculate_avg_fixed <- function(df) {
mean(df[[2]])
}
# Fix 2: Using pull() (the tidyverse way)
library(dplyr)
calculate_avg_tidy <- function(df) {
mean(pull(df, value))
}
calculate_avg_fixed(readings) # 23.83333
Creating Data Manually with tribble
Most of the time, you're loading CSVs or SQL queries. But sometimes you need to hard-code a small lookup table or a set of parameters directly into your script. Writing tibble(col1 = c(...), col2 = c(...)) is tedious and hard to read. This is where tribble() (transposed tibble) comes in.
I love tribble() because it lets you lay out the data exactly how it will look in the final table. It's much more intuitive for anyone reading your code later.
# Imagine we're setting up a conversion rate table
conversion_rates <- tribble(
~currency, ~rate, ~symbol,
"USD", 1.0, "$",
"EUR", 0.92, "€",
"GBP", 0.79, "£"
)
print(conversion_rates)
Strictness as a Feature
You might find tibbles "annoying" because they don't allow partial matching. In base R, if you have a column named temperature_celsius, you can actually access it using df$temp and R will guess what you mean. Tibbles refuse to do this. They will return NULL instead.
This feels like a hurdle at first, but in a production environment, partial matching is a liability. It's far better for your code to fail immediately because of a typo than to silently grab the wrong column because it "looked close enough." When you're writing professional software, explicit is always better than implicit.
Handling Column Names and Types
One last thing to keep in mind: tibbles never change the type of your input. If you pass a character vector into a tibble, it stays a character vector. It doesn't try to be "helpful" by converting things into factors (which was a huge headache in older versions of base R).
If you find yourself needing to clean up messy column names—like those annoying spaces or special characters that come from Excel imports—don't do it manually. While tibble provides the structure, I always pair it with the janitor package's clean_names() function to ensure my column headers are programmatically friendly.
📋 Practical Task
Exercise: Building a Project Resource Matrix
You are tasked with creating a small reference table for a project management tool. Instead of loading a file, you need to hard-code this data into your script for portability.
Requirements:
- Use the
tribble()function to create a tibble namedteam_resources. - The table must have three columns:
staff_name(character),role(character), andhourly_rate(numeric). - Add at least three rows of data (e.g., "Alice", "Lead Dev", 150).
- Once the tibble is created, write a line of code that extracts the
hourly_ratecolumn as a numeric vector (not a tibble) and calculates the average rate usingmean().
Expected Output: A single numeric value representing the average of the rates you entered.
There are no comments for now.