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
53: tidyselect Helpers
How do I grab a bunch of columns that follow a naming pattern without typing them all?
This is where you'll spend most of your time with tidyselect. When you're dealing with a dataset that has dozens of columns—like a climate dataset with temp_jan, temp_feb, temp_mar, and so on—typing every single name is a waste of your life. I usually reach for starts_with(), ends_with(), or contains().
# Let's say we have a dataframe called 'weather_df'
# I only want the temperature columns
weather_df %>%
select(starts_with("temp"))
# Or maybe I want everything related to January across different metrics
weather_df %>%
select(ends_with("_jan"))
One thing to keep in mind: these helpers are case-insensitive by default. If you have columns named Temp_Jan and temp_jan, starts_with("temp") will grab both. If you need to be strict, you can pass ignore.case = FALSE as a second argument. I rarely use it, but it's there if your naming convention is weirdly specific.
What's the difference between all_of() and any_of() when using a character vector?
You'll often run into a situation where your column names are stored in a variable—maybe a character vector you built dynamically. If you just throw that vector into select(), R sometimes gets confused about whether you're referring to a column name or the vector itself. That's why we use all_of() and any_of().
target_cols <- c("temp_jan", "precip_jan", "wind_jan")
# This will throw an error if even ONE of these columns is missing
weather_df %>%
select(all_of(target_cols))
# This is the "safe" version. It grabs whatever it finds and ignores the rest.
weather_df %>%
select(any_of(target_cols))
I almost always prefer any_of() when writing production code. It prevents your entire pipeline from crashing just because a data provider decided to rename wind_jan to wind_speed_jan in this month's export.
Can I select columns based on their data type?
Absolutely. This is a lifesaver when you need to perform an operation—like scaling or rounding—on every numeric column without knowing their names. You use the where() helper. It basically lets you pass a predicate function (a function that returns TRUE or FALSE) to filter the columns.
# Grab only the numeric columns for a correlation matrix
weather_df %>%
select(where(is.numeric))
# Or, if you want to find all character columns that might need cleaning
weather_df %>%
select(where(is.character))
I've found this incredibly useful when combined with across() in a mutate() call, but for just selecting, where() is the way to go.
Is matches() just a fancy way to do regex?
Pretty much. While starts_with() is great for simple stuff, matches() gives you the full power of regular expressions. If you have a complex naming scheme—like columns that start with a letter, followed by an underscore, and ending in a four-digit year—the basic helpers won't cut it.
# Select columns that match a pattern: word, underscore, then 4 digits
# Example: "temp_2021", "precip_2022"
weather_df %>%
select(matches("^[a-z]+_\\d{4}$"))
Fair warning: regex can get ugly fast. If you find yourself writing a 50-character regex string just to select three columns, stop and ask if you can just rename your data first. It'll make your code much easier for your future self to read.
📋 Practical Task
Cleaning the Global Sensor Dataset
You have been handed a messy dataframe called sensor_data. It contains the following columns: sensor_id, location_city, location_country, reading_temp_C, reading_humidity, reading_pressure, last_updated_date, and status_code.
Write a code snippet using dplyr::select() and tidyselect helpers to create a new dataframe called numeric_readings that meets these three criteria:
- It includes the
sensor_idcolumn. - It includes all columns that start with the word
reading. - It excludes any columns that are not numeric (aside from
sensor_id).
Hint: Think about whether you should use starts_with() first or where(is.numeric) to ensure you don't accidentally keep non-numeric columns that might happen to start with "reading".
There are no comments for now.